作為Android4.4 KitKat系統的新特性之一“Full-screen Immersive Mode(全屏沉浸模式)”。當啟用該模式,應用程序的界面將占據整個屏幕,系統自動將隱藏系統的狀態欄和導航欄,讓應用程序內容可以在最大顯示范圍呈現,增加大屏體驗,而當需要查看通知的時候只需要從頂部向下滑動就能呼出通知欄。
普通全屏模式(Fullscreen)、沉浸模式 (Immersive)、黏性沉浸模式 (Sticky Immersive)
說到全屏,我們先看看如何實現普通的全屏模式。
參考資料:
http://libufan.sinaapp.com/?p=55
http://blog.sina.com.cn/s/blog_9f3cecc50101gw7j.html
http://blog.csdn.net/heng615975867/article/details/9031931
接下來我們一起看看如何實現Android4.4系統帶來的全屏沉浸模式。
參考資料:
https://developer.android.com/intl/zh-cn/training/system-ui/immersive.html#nonsticky
http://www.cnblogs.com/zhengxt/p/3508485.html
http://www.tuicool.com/articles/RVRneq
官方文檔上說全屏沉浸模式有兩種選擇,一種是
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 void hideSystemUI() { // 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 void showSystemUI() { mDecorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); }
@Override public void onWindowFocusChanged(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 void toggleHideyBar() { // 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) }