沉浸式状态栏
//View.SYSTEM_UI_FLAG_FULLSCREEN 全屏
//View.SYSTEM_UI_FLAG_LAYOUT_STABLE 显示系统状态栏
//View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 隐藏底部导航栏
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//完全沉浸
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//一般来说
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);
//代码设置,也可以在manifest中设置主题为 noActionBar
getSupportActionBar().hide();
}
华为手机适配
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
设置沉浸式界面,另外仍需要在xml中设置fitsSystemWindows,Constants.isDarkBG 描述是否为暗色背景
*/
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|(Constants.isDarkBG ? 0 : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}