在搭载 Android 15 或更高版本的设备上以 SDK 35 或更高版本为目标平台后,您的应用将全屏显示。该窗口通过在系统栏后面绘制,横跨显示屏的整个宽度和高度。系统栏包括状态栏、字幕栏和导航栏。
许多应用都有顶部应用栏。顶部应用栏应拉伸到屏幕的顶部边缘,并显示在状态栏后面。(可选)顶部应用栏可以在内容滚动时缩小到状态栏的高度。
许多应用还有一个底部应用栏或底部导航栏。这些栏还应延伸到屏幕底部边缘,并显示在导航栏后面。否则,应用应在导航栏后面显示滚动内容。
在应用中实现全屏布局时,请注意以下几点:
- 启用无边框显示屏
- 处理任何视觉重叠。
- 考虑在系统栏后面显示纱罩。
启用无边框显示屏
如果您的应用以 SDK 35 或更高版本为目标平台,系统会自动为搭载 Android 15 或更高版本的设备启用全屏。
如需在以前的 Android 版本上启用全屏,请执行以下操作:
在应用或模块的
build.gradle
文件中为androidx.activity
库添加一个依赖项:Kotlin
dependencies { val activity_version =
activity_version
// Java language implementation implementation("androidx.activity:activity:$activity_version") // Kotlin implementation("androidx.activity:activity-ktx:$activity_version") }Groovy
dependencies { def activity_version =
activity_version
// Java language implementation implementation 'androidx.activity:activity:$activity_version' // Kotlin implementation 'androidx.activity:activity-ktx:$activity_version' }将
enableEdgeToEdge
扩展函数导入您的应用:
通过在 Activity
的 onCreate
中调用 enableEdgeToEdge
来手动启用无边框。应在 setContentView
之前调用它。
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) ... }
Java
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { EdgeToEdge.enable(this); super.onCreate(savedInstanceState); ... }
默认情况下,enableEdgeToEdge()
会使系统栏处于透明状态,但在“三按钮”导航模式下除外,在该模式下,状态栏会使用半透明的纱罩。系统图标和纱罩的颜色会根据系统的浅色或深色主题进行调整。
enableEdgeToEdge()
函数会自动声明应用应无边框布局,并调整系统栏的颜色。
如需在不使用 enableEdgeToEdge()
函数的情况下在应用中启用全屏显示,请参阅手动设置无边框显示屏。
使用边衬区处理重叠
应用的某些视图可能会在系统栏后面绘制,如图 3 所示。
您可以通过回应边衬区来解决重叠问题,边衬区指定屏幕的哪些部分与系统界面(例如导航栏或状态栏)相交。相交可能意味着在内容上方显示,但也可以将系统手势告知您的应用。
适用于全屏显示应用的边衬区类型包括:
系统栏边衬区:最适合可点按且不得被系统栏遮挡的视图。
刘海屏边衬区:适用于可能因设备形状而刘海屏的区域。
系统手势边衬区:适用于系统使用的手势导航区域,这些区域的优先级高于应用。
系统栏边衬区
系统栏边衬区是最常用的边衬区类型。它们代表系统界面在应用上方的 Z 轴中显示的区域。它们最适合用于移动或填充应用中可点按且不得被系统栏遮挡的视图。
例如,图 3 中的悬浮操作按钮 (FAB) 会被导航栏部分遮挡:
为避免在手势模式或按钮模式下出现这种视觉重叠,您可以使用 getInsets(int)
和 WindowInsetsCompat.Type.systemBars()
来增加视图的外边距。
以下代码示例展示了如何实现系统栏边衬区:
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(fab) { v, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) // Apply the insets as a margin to the view. This solution sets // only the bottom, left, and right dimensions, but you can apply whichever // insets are appropriate to your layout. You can also update the view padding // if that's more appropriate. v.updateLayoutParams<MarginLayoutParams> { leftMargin = insets.left, bottomMargin = insets.bottom, rightMargin = insets.right, } // Return CONSUMED if you don't want want the window insets to keep passing // down to descendant views. WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(fab, (v, windowInsets) -> { Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); // Apply the insets as a margin to the view. This solution sets only the // bottom, left, and right dimensions, but you can apply whichever insets are // appropriate to your layout. You can also update the view padding if that's // more appropriate. MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams(); mlp.leftMargin = insets.left; mlp.bottomMargin = insets.bottom; mlp.rightMargin = insets.right; v.setLayoutParams(mlp); // Return CONSUMED if you don't want want the window insets to keep passing // down to descendant views. return WindowInsetsCompat.CONSUMED; });
如果将此解决方案应用于图 3 中所示的示例,将导致按钮模式下没有视觉重叠,如图 4 所示:
这同样适用于手势导航模式,如图 5 所示:
刘海屏边衬区
某些设备有刘海屏。通常,刘海屏位于屏幕顶部并包含在状态栏中。当设备屏幕处于横屏模式时,刘海屏可能位于垂直边缘。根据应用在屏幕上显示的内容,您应实现内边距以避免出现刘海屏,因为默认情况下,应用会在刘海屏上绘制内容。
例如,许多应用屏幕都会显示项目列表。请勿使用刘海屏或系统栏遮挡列表项。
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(binding.recyclerView) { v, insets -> val bars = insets.getInsets( WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() ) v.updatePadding( left = bars.left, top = bars.top, right = bars.right, bottom = bars.bottom, ) WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(mBinding.recyclerView, (v, insets) -> { WindowInsetsCompat bars = insets.getInsets( WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout() ); v.setPadding(bars.left, bars.top, bars.right, bars.bottom); return WindowInsetsCompat.CONSUMED; });
通过计算系统栏和刘海屏类型的逻辑“或”来确定 WindowInsetsCompat
的值。
将 clipToPadding
设置为 RecyclerView
,使内边距随列表项一起滚动。这样,当用户滚动时,这些项就会移到系统栏后面,如以下示例所示。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
系统手势边衬区
系统手势边衬区表示窗口区域,在这些区域中,系统手势优先于应用。在图 6 中,这些区域显示为橙色:
与系统栏边衬区一样,您可以将 getInsets(int)
与 WindowInsetsCompat.Type.systemGestures()
结合使用,避免系统手势边衬区重叠。
使用这些边衬区可将可滑动视图移出边缘或向边缘移动。常见用例包括底部动作条、游戏中滑动,以及使用 ViewPager2
实现的轮播界面。
在 Android 10 或更高版本中,系统手势边衬区包含主屏幕手势的底部边衬区,以及用于返回手势的左侧和右侧边衬区:
以下代码示例展示了如何实现系统手势边衬区:
Kotlin
ViewCompat.setOnApplyWindowInsetsListener(view) { view, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures()) // Apply the insets as padding to the view. Here, set all the dimensions // as appropriate to your layout. You can also update the view's margin if // more appropriate. view.updatePadding(insets.left, insets.top, insets.right, insets.bottom) // Return CONSUMED if you don't want the window insets to keep passing down // to descendant views. WindowInsetsCompat.CONSUMED }
Java
ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> { Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures()); // Apply the insets as padding to the view. Here, set all the dimensions // as appropriate to your layout. You can also update the view's margin if // more appropriate. view.setPadding(insets.left, insets.top, insets.right, insets.bottom); // Return CONSUMED if you don't want the window insets to keep passing down // to descendant views. return WindowInsetsCompat.CONSUMED; });
Material 组件
许多基于视图的 Android Material 组件 (com.google.android.material){:.external} 会自动处理边衬区,包括 BottomAppBar
、BottomNavigationView
、NavigationRailView
和 NavigationView
不过,AppBarLayout
不会自动处理边衬区。添加 android:fitsSystemWindows="true"
来处理顶部边衬区或使用 setOnApplyWindowInsetsListener
。
了解如何使用 Compose 中的 Material 组件处理边衬区。
沉浸模式
有些内容在全屏模式下效果最佳,可以为用户带来更身临其境的体验。您可以使用 WindowInsetsController
和 WindowInsetsControllerCompat
库在沉浸模式下隐藏系统栏:
Kotlin
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView) // Hide the system bars. windowInsetsController.hide(Type.systemBars()) // Show the system bars. windowInsetsController.show(Type.systemBars())
Java
Window window = getWindow(); WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(window, window.getDecorView()); if (windowInsetsController == null) { return; } // Hide the system bars. windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()); // Show the system bars. windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
如需详细了解如何实现此功能,请参阅隐藏系统栏以实现沉浸模式。
其他资源
如需详细了解 WindowInsets
、手势导航以及边衬区的工作原理,请参阅以下参考文档: