有些内容最好在全屏模式下观看,状态栏或导航栏上不会显示任何指示符。例如视频、游戏、图库、图书和演示文稿幻灯片。这称为沉浸模式。本页介绍了如何利用全屏模式吸引用户更深入地互动。
沉浸模式可帮助用户避免在游戏过程中意外退出,并提供沉浸式的图片、视频和图书体验。但是,请留意用户进入和退出应用以查看通知、执行随机搜索或执行其他操作的频率。由于沉浸模式会导致用户无法轻松访问系统导航,因此,只有当对用户的好处远不止使用额外的屏幕空间时,才使用沉浸模式。
使用 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
通过系统手势(例如从隐藏该栏的屏幕边缘滑动)暂时显示隐藏的系统栏。这些瞬态系统栏会叠加在应用内容之上,可能具有一定的透明度,并在短暂超时后自动隐藏。