调用 enableEdgeToEdge 会封装真正实现
向后兼容性所需的逻辑,因此是设置
全面屏显示的首选方式。如需了解使用 enableEdgeToEdge 实现全面屏显示的现代方式,请参阅 Compose 和 视图 文档,而不是本指南。
虽然不建议这样做,但如果您的应用必须手动设置全面屏显示 您可以按照以下步骤操作:
- 调用
WindowCompat.setDecorFitsSystemWindows(window, false)。 - 将系统栏设置为透明。
- 处理插边。
以全屏模式布局应用
使用 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); }
更改系统栏的颜色
在为 Android 14 及更低版本手动创建全面屏布局时,您的应用 还必须将系统栏设为透明。
您可以修改 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>
您可以使用 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);
处理插边
最后,您的应用必须处理插边,以使关键界面避开 系统栏和显示屏凹口。如需了解如何处理插边,请参阅 Compose 和 视图 文档。