在 View 和 Compose 中使用内边距
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果您的应用同时包含 Compose 和 View 代码,您可能需要明确指定每个代码应占用哪些系统边衬区,并确保边衬区会分派给同级视图。
替换默认边衬区
如果您的屏幕在同一层次结构中同时包含 View 和 Compose 代码,您可能需要替换默认边衬区。在这种情况下,您需要明确指定哪个视图应使用边衬区,哪个视图应忽略边衬区。
例如,如果最外层的布局是 Android View 布局,您应该在 View 系统中使用边衬区,并在 Compose 中忽略它们。或者,如果最外层的布局是可组合项,您应该在 Compose 中使用边衬区,并相应地填充 AndroidView
可组合项。
默认情况下,每个 ComposeView
在 WindowInsetsCompat
级消耗量下会消耗所有插页式广告。如需更改此默认行为,请将 AbstractComposeView.consumeWindowInsets
设置为 false
。
视图的向后兼容的边衬区调度
如果您的应用包含视图代码,您可能需要确认插边是否已分派给搭载 Android 10(API 级别 29)或更低版本的设备上的同级视图。如需了解详情,请参阅全屏视图指南。
系统栏图标
调用 enableEdgeToEdge
可确保在设备主题发生更改时更新系统栏图标颜色。
在实现无边框布局时,您可能需要手动更新系统栏图标颜色,使其与应用的背景形成对比。例如,如需创建浅色状态栏图标,请执行以下操作:
Kotlin
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = false
Java
WindowCompat.getInsetsController(window, window.getDecorView())
.setAppearanceLightStatusBars(false);
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Use insets in Views and Compose\n\nIf your app contains both Compose and View code, you may need to be explicit\nabout which system insets each one should consume and ensure that insets are\ndispatched to sibling views.\n\nOverriding default insets\n-------------------------\n\nYou may need to override default insets when your screen has both Views and\nCompose code in the same hierarchy. In this case, you need to be explicit in\nwhich one should consume the insets, and which one should ignore them.\n\nFor example, if your outermost layout is an Android View layout, you should\nconsume the insets in the View system and ignore them for Compose.\nAlternatively, if your outermost layout is a composable, you should consume the\ninsets in Compose, and pad the `AndroidView` composables accordingly.\n\nBy default, each `ComposeView` consumes all insets at the\n`WindowInsetsCompat` level of consumption. To change this default behavior, set\n[`AbstractComposeView.consumeWindowInsets`](/reference/kotlin/androidx/compose/ui/platform/AbstractComposeView#(androidx.compose.ui.platform.AbstractComposeView).consumeWindowInsets())\nto `false`.\n\nBackward compatible inset dispatching for views\n-----------------------------------------------\n\nIf your app contains Views code, you may need to confirm that insets are dispatched\nto sibling views on devices that run Android 10 (API level 29) or lower. See the\n[edge-to-edge Views guide](/develop/ui/views/layout/edge-to-edge#backward-compatible-dispatching)\nfor more information.\n\nSystem bar icons\n----------------\n\nCalling `enableEdgeToEdge` ensures system bar icon colors update when the device\ntheme changes.\n\nWhile going edge-to-edge, you might need to manually update the system bar icon\ncolors so they contrast with your app's background. For example, to create light\nstatus bar icons: \n\n### Kotlin\n\n```kotlin\nWindowCompat.getInsetsController(window, window.decorView)\n .isAppearanceLightStatusBars = false\n```\n\n### Java\n\n```java\nWindowCompat.getInsetsController(window, window.getDecorView())\n .setAppearanceLightStatusBars(false);\n```"]]