部分內容在全螢幕模式下可帶來最佳體驗,而狀態列或導覽列不會顯示任何指標。例如影片、遊戲、圖片圖片庫、書籍和簡報投影片。這就是所謂的「沈浸模式」。本頁面說明如何透過全螢幕顯示更深入的內容,吸引使用者的目光。
沉浸模式可協助使用者在遊戲期間意外退出,並提供沉浸式體驗,讓你盡情享受圖片、影片和書籍。不過,請留意使用者進入及離開應用程式查看通知的頻率、執行不預期的搜尋,或是執行其他操作的頻率。由於沉浸模式會導致使用者無法輕鬆存取系統導覽,因此請只在有助使用者體驗帶來的益處,光使用額外的螢幕空間時,才使用沉浸模式。
使用 WindowInsetsControllerCompat.hide()
隱藏系統列,而 WindowInsetsControllerCompat.show()
則可恢復顯示。
下列程式碼片段示範如何設定隱藏及顯示系統資訊列的按鈕。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { ... val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView) // Configure the behavior of the hidden system bars. windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE // Add a listener to update the behavior of the toggle fullscreen button when // the system bars are hidden or revealed. ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets -> // You can hide the caption bar even when the other system bars are visible. // To account for this, explicitly check the visibility of navigationBars() // and statusBars() rather than checking the visibility of systemBars(). if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) { binding.toggleFullscreenButton.setOnClickListener { // Hide both the status bar and the navigation bar. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) } } else { binding.toggleFullscreenButton.setOnClickListener { // Show both the status bar and the navigation bar. windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) } } ViewCompat.onApplyWindowInsets(view, windowInsets) } }
Java
@Override protected void onCreate(Bundle savedInstanceState) { ... WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView()); // Configure the behavior of the hidden system bars. windowInsetsController.setSystemBarsBehavior( WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE ); // Add a listener to update the behavior of the toggle fullscreen button when // the system bars are hidden or revealed. ViewCompat.setOnApplyWindowInsetsListener( getWindow().getDecorView(), (view, windowInsets) -> { // You can hide the caption bar even when the other system bars are visible. // To account for this, explicitly check the visibility of navigationBars() // and statusBars() rather than checking the visibility of systemBars(). if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) { binding.toggleFullscreenButton.setOnClickListener(v -> { // Hide both the status bar and the navigation bar. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()); }); } else { binding.toggleFullscreenButton.setOnClickListener(v -> { // Show both the status bar and the navigation bar. windowInsetsController.show(WindowInsetsCompat.Type.systemBars()); }); } return ViewCompat.onApplyWindowInsets(view, windowInsets); }); }
您可以視需要指定要隱藏的系統資訊列類型,以及決定使用者與這些列互動時的行為。
指定要隱藏的系統資訊列
如要指定要隱藏的系統資訊列類型,請將下列其中一項參數傳遞至 WindowInsetsControllerCompat.hide()
。
使用
WindowInsetsCompat.Type.systemBars()
隱藏兩個系統資訊列。使用
WindowInsetsCompat.Type.statusBars()
可只顯示狀態列。如果只想隱藏導覽列,請使用
WindowInsetsCompat.Type.navigationBars()
。
指定隱藏系統資訊列的行為
使用 WindowInsetsControllerCompat.setSystemBarsBehavior()
指定隱藏系統列在使用者與其互動時的行為。
使用
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
即可在對應螢幕上顯示「任何」使用者互動時隱藏的系統資訊列。使用
WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
即可在任何系統手勢 (例如隱藏長條的螢幕邊緣) 上顯示隱藏的系統資訊列。使用
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
即可透過系統手勢 (例如從隱藏長條的螢幕邊緣滑動) 暫時顯示隱藏的系統資訊列。這些暫時性的系統資訊列會疊加在應用程式內容上,可能具備一定程度的透明度,且會在短暫逾時後自動隱藏。