Android导航栏会根据条件

时间:2017-09-26 13:35:43

标签: android android-toolbar

我可以根据视图是否在屏幕上来决定是否更改导航栏的颜色。该方法已编写,但我不知道如何调用它。我通过按钮单击事件调用此方法,但这非常困难。希望他自动拨打电话,而不是点击活动电话,我该怎么办?

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));
        }
}

1 个答案:

答案 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"/>