隐藏系统栏以实现沉浸式模式

某些内容最适合在全屏模式下体验,没有状态栏或导航栏上的任何指示器。例如,视频、游戏、图片库、书籍和演示文稿幻灯片。这被称为沉浸模式。本页展示了如何使用全屏模式更深入地吸引用户参与内容。

图 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() 指定隐藏的系统栏在用户与它们交互时的行为。