2017-09-26 57 views
0

我有一種方法可以根據是否在屏幕上決定是否更改導航欄的顏色。該方法已被編寫,但我不知道如何調用它。我用按鈕點擊事件調用這個方法,但這非常困難。希望他自動打電話,而不是點擊事件通話,我該怎麼辦?Android導航欄根據條件自動更改顏色

public class Index extends Activity implements View.OnClickListener{ 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.index); 
} 

private void changeNavigationColour(View view){ 
     Point p = new Point(); 
     getWindowManager().getDefaultDisplay().getSize(p); 
     int[] loacation = new int[2]; 
     view.getLocationInWindow(loacation); 
     Toast.makeText(this, Arrays.toString(loacation),Toast.LENGTH_SHORT).show(); 
     Rect ivRect = new Rect(view.getLeft(),view.getTop(),view.getRight(),view.getBottom()); 
     LinearLayout head = (LinearLayout) findViewById(R.id.index_head); 
     if(view.getLocalVisibleRect(ivRect)){ 
      //Change the navigation color 
      head.setBackgroundColor(getResources().getColor(R.color.transparent)); 
     }else { 
      //Change the navigation color 
      head.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); 
     } 
} 
+0

發佈您迄今爲止試過的幫助瞭解問題 –

+0

向我們顯示您的代碼。 –

+0

你需要更清楚地瞭解這個「標準」的變化。它是什麼?你怎麼得到它? –

回答

0

基於您的評論看來你需要根據手機通話狀態

對於調用changeNavigationColour(View view)可以使用PhoneStateListener

public class Index extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.index); 

     TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new myPhoneStateChangeListener(),PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    public class myPhoneStateChangeListener extends PhoneStateListener { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      super.onCallStateChanged(state, incomingNumber); 
      // call changeNavigationColour based on the state 

     } 
    } 
} 

你也需要有這樣的你清單

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>