在沈浸模式下隱藏系統資訊列

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

圖 1. 沉浸模式示例。

沉浸模式可避免使用者在遊戲過程中意外離開,並提供沉浸式體驗,讓使用者盡情享受圖片、影片和書籍。不過,請留意使用者經常進出應用程式,以便查看通知、臨時搜尋或採取其他行動的頻率。由於沉浸模式會讓使用者無法輕鬆存取系統導覽功能,因此請只在使用者體驗的效益超越單純使用額外螢幕空間時,才使用沉浸模式。

使用 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()

指定隱藏系統資訊列的行為

使用 WindowInsetsControllerCompat.setSystemBarsBehavior() 指定隱藏的系統列在使用者與其互動時的行為。