在 View 和 Compose 中使用內嵌
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如果應用程式同時包含 Compose 和 View 程式碼,您可能需要明確指出每個系統插邊應使用的插邊,並確保插邊會傳送至同層級檢視區塊。
覆寫預設插邊
如果畫面在同一階層中同時有 View 和 Compose 程式碼,您可能需要覆寫預設插邊。在這種情況下,您必須明確指出哪個項目應使用插邊,哪個項目應忽略插邊。
舉例來說,如果最外層的版面配置是 Android View 版面配置,您應該在 View 系統中取用插邊,並在 Compose 中忽略插邊。或者,如果最外層的版面配置是可組合函式,您應該在 Compose 中使用插邊,並相應地填補 AndroidView
可組合函式。
根據預設,每個 ComposeView
會耗用所有插邊,且耗用量為 WindowInsetsCompat
級別。如要變更這項預設行為,請將
AbstractComposeView.consumeWindowInsets
設為 false
。
檢視區塊的向後相容插邊調度
如果應用程式包含 Views 程式碼,您可能需要在搭載 Android 10 (API 級別 29) 以下版本的裝置上,確認插邊是否已分派至同層級檢視區塊。詳情請參閱無邊框檢視區塊指南。
系統資訊列圖示
呼叫 enableEdgeToEdge
可確保裝置主題變更時,系統資訊列圖示顏色會更新。
採用無邊框設計時,您可能需要手動更新系統資訊列圖示顏色,確保與應用程式背景形成對比。舉例來說,如要建立淺色狀態列圖示,請執行下列操作:
Kotlin
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = false
Java
WindowCompat.getInsetsController(window, window.getDecorView())
.setAppearanceLightStatusBars(false);
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間: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```"]]