Android导航栏根据条件自动更改颜色
问题描述:
我有一种方法可以根据是否在屏幕上决定是否更改导航栏的颜色。该方法已被编写,但我不知道如何调用它。我用按钮点击事件调用这个方法,但这非常困难。希望他自动打电话,而不是点击事件通话,我该怎么办?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));
}
}
答
基于您的评论看来你需要根据手机通话状态
对于调用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"/>
发布您迄今为止试过的帮助了解问题 –
向我们显示您的代码。 –
你需要更清楚地了解这个“标准”的变化。它是什么?你怎么得到它? –