觀看某些內容時,建議以全螢幕模式觀看,並且在狀態列或導覽列上不顯示任何指標。例如影片、遊戲、圖片庫、書籍和簡報投影片。這稱為 沉浸模式。本頁說明如何讓使用者在全螢幕模式下更深入地與內容互動。

沉浸模式可避免使用者在遊戲過程中誤觸退出按鈕,並提供沉浸式體驗,讓使用者盡情享受圖片、影片和書籍。不過,請留意使用者進入及離開應用程式查看通知的頻率。 經常進行搜尋或執行其他動作。因為沉浸模式 導致使用者無法輕鬆存取系統導覽功能,請只使用沉浸模式 不只使用額外螢幕,這對使用者體驗有助益 空白鍵。
使用 WindowInsetsControllerCompat.hide()
隱藏系統資訊列和 WindowInsetsControllerCompat.show()
才能再次成功
以下程式碼片段示範如何設定隱藏及顯示按鈕 系統資訊列。
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) } }
@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
利用系統手勢暫時顯示隱藏的系統資訊列,例如 從畫面邊緣滑動的視窗邊緣。這些 暫時性系統資訊列會疊加顯示在應用程式內容上, 且會在短暫逾時後自動隱藏。