您可以通过调用 enableEdgeToEdge
在应用中启用全屏显示。这对于大多数应用来说应该足够了。本指南介绍了如果您的应用需要不使用 enableEdgeToEdge
来启用全屏显示的方法。
将应用布局为全屏
使用 WindowCompat.setDecorFitsSystemWindows(window, false)
将您的应用布局在系统栏后面,如以下代码示例所示
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) WindowCompat.setDecorFitsSystemWindows(window, false) }
Java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowCompat.setDecorFitsSystemWindows(getWindow(), false); }
更改系统栏的颜色
在全屏布局中运行时,您的应用需要更改系统栏的颜色,以便其下方的内容可见。应用完成此步骤后,系统将在手势导航模式和按钮模式下处理用户界面的所有视觉保护。
- 手势导航模式:系统会应用动态颜色适配,其中系统栏的内容会根据其后面的内容更改颜色。在以下示例中,导航栏中的滑块在位于浅色内容上方时变为深色,在位于深色内容上方时变为浅色。
- 按钮模式:系统会在系统栏后面应用半透明的 幕布(适用于 API 级别 29 或更高版本)或透明系统栏(适用于 API 级别 28 或更早版本)。

- 状态栏内容颜色:控制状态栏内容的颜色,例如时间和图标。

您可以编辑 themes.xml
文件来设置导航栏的颜色,并且可以选择将状态栏设置为透明并将状态栏内容颜色设置为深色。
<!-- values-v29/themes.xml -->
<style name="Theme.MyApp">
<item name="android:navigationBarColor">
@android:color/transparent
</item>
<!-- Optional: set to transparent if your app is drawing behind the status bar. -->
<item name="android:statusBarColor">
@android:color/transparent
</item>
<!-- Optional: set for a light status bar with dark content. -->
<item name="android:windowLightStatusBar">
true
</item>
</style>
您可以直接使用 WindowInsetsController
API,但我们强烈建议在可能的情况下使用支持库 WindowInsetsControllerCompat
。您可以使用 WindowInsetsControllerCompat
API 而非 theme.xml
来控制状态栏的内容颜色。为此,请使用 setAppearanceLightNavigationBars()
函数,传入 true
将导航栏的前景颜色更改为浅色,或传入 false
恢复为默认颜色。
Kotlin
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) windowInsetsController?.isAppearanceLightNavigationBars = true
Java
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView()); if (windowInsetsController == null) { return; } windowInsetsController.setAppearanceLightNavigationBars(true);