作为Android4.4 KitKat系统的新特性之一“Full-screen Immersive Mode(全屏沉浸模式)”。当启用该模式,应用程序的界面将占据整个屏幕,系统自动将隐藏系统的状态栏和导航栏,让应用程序内容可以在最大显示范围呈现,增加大屏体验,而当需要查看通知的时候只需要从顶部向下滑动就能呼出通知栏。
普通全屏模式(Fullscreen)、沉浸模式 (Immersive)、黏性沉浸模式 (Sticky Immersive)
说到全屏,我们先看看如何实现普通的全屏模式。
参考资料:
接下来我们一起看看如何实现Android4.4系统带来的全屏沉浸模式。
参考资料:
官方文档上说全屏沉浸模式有两种选择,一种是
View.SYSTEM_UI_FLAG_IMMERSIVE
这个选择通过组合
View.SYSTEM_UI_FLAG_FULLSCREEN
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
将这个组合通过
setSystemUiVisibility
设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。但是这个选择通过顶部下滑就恢复原状,同时自动清除这三个标志。
另一种是
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
同样设置以后可以实现全屏沉浸模式,并且隐藏顶部状态栏和底部的虚拟导航键。与第一种不同的是此时通过顶部下滑会显示出透明的状态栏和虚拟导航栏,在一段时间以后自动回复成全屏沉浸模式,需要手动清除这三个标志回复原状。
最后做一下扩展
//This snippet hides the system bars.
private voidhideSystemUI() {//Set the IMMERSIVE flag.//Set the content to appear under the system bars so that the content//doesn't resize when the system bars hide and show.
mDecorView.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 //hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN //hide status bar
|View.SYSTEM_UI_FLAG_IMMERSIVE);
}//This snippet shows the system bars. It does this by removing all the flags//except for the ones that make the content appear under the system bars.
private voidshowSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
@Overridepublic voidonWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);if(hasFocus) {
decorView.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);}
}
注意标志位的版本判断:
public voidtoggleHideyBar() {//BEGIN_INCLUDE (get_current_ui_flags)//The UI options currently enabled are represented by a bitfield.//getSystemUiVisibility() gives us that bitfield.
int uiOptions =getActivity().getWindow().getDecorView().getSystemUiVisibility();int newUiOptions =uiOptions;//END_INCLUDE (get_current_ui_flags)//BEGIN_INCLUDE (toggle_ui_flags)//boolean isImmersiveModeEnabled =//((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
boolean isImmersiveModeEnabled =((uiOptions| View.SYSTEM_UI_FLAG_IMMERSIVE) ==uiOptions);if(isImmersiveModeEnabled) {
Log.i(TAG,"Turning immersive mode mode off.");
}else{
Log.i(TAG,"Turning immersive mode mode on.");
}//Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions^=View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}//Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions^=View.SYSTEM_UI_FLAG_FULLSCREEN;
}//Immersive mode: Backward compatible to KitKat.//Note that this flag doesn't do anything by itself, it only augments the behavior//of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample//all three flags are being toggled together.//Note that there are two immersive mode UI flags, one of which is referred to as "sticky".//Sticky immersive mode differs in that it makes the navigation and status bars//semi-transparent, and the UI flag does not get cleared when the user interacts with//the screen.
if (Build.VERSION.SDK_INT >= 18) {//newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
newUiOptions ^=View.SYSTEM_UI_FLAG_IMMERSIVE;
}
getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);//END_INCLUDE (set_ui_flags)
}