Android如何为所有活动全局设置标志

时间:2019-05-06 11:26:32

标签: java android android-intent

我遇到的问题是,例如,每次我每次开始新的活动时都这样:

Intent exchangedetail = new Intent(getActivity(), ExchangeDetail.class);
exchangedetail.putExtra("key", web[+ position]);
getActivity().startActivity(exchangedetail);

我必须每次都这样设置Light NavBar和Status Bar:

//Setup Status Bar and Nav Bar white if supported
View decorView = getWindow().getDecorView();
Window win = getWindow();

if(Build.VERSION.SDK_INT >= 27) {
    decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
            View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
else {
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

是否有任何方法可以在MainActivity中设置此标志,并且无论何时新的Intent / Activity都使用此标志,因此我不必在每次调用新Intent时都对其进行设置?我认为style.xml是可以的,但是我需要在我的App的StartUp上自动设置这些标志。

1 个答案:

答案 0 :(得分:3)

您可以使用基本活动,然后可以扩展您的每个活动

public class BaseActviity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   //Setup Status Bar and Nav Bar white if supported
    View decorView = getWindow().getDecorView();
    Window win = getWindow();

    if(Build.VERSION.SDK_INT >= 27) {
        decorView.setSystemUiVisibility(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
                View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
    else if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 27) {
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    else {
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

}
}

class MainActivty extends BaseActviity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}