Sử dụng phần lồng ghép trong Khung hiển thị và Compose
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nếu ứng dụng của bạn chứa cả mã Compose và mã Khung hiển thị, thì bạn có thể cần phải xác định rõ ràng hệ thống phần lồng ghép nào mà mỗi mã sẽ sử dụng và đảm bảo rằng phần lồng ghép được gửi đến các khung hiển thị cùng cấp.
Ghi đè phần lồng ghép mặc định
Bạn có thể cần ghi đè phần lồng ghép mặc định khi màn hình của bạn có cả Khung hiển thị và mã Compose trong cùng một hệ phân cấp. Trong trường hợp này, bạn cần phải xác định rõ ràng thành phần nào sẽ sử dụng phần lồng ghép và thành phần nào sẽ bỏ qua phần lồng ghép.
Ví dụ: nếu bố cục ngoài cùng của bạn là bố cục Khung hiển thị Android, bạn nên sử dụng phần lồng ghép trong hệ thống Khung hiển thị và bỏ qua phần lồng ghép đó cho Compose.
Ngoài ra, nếu bố cục ngoài cùng của bạn là một thành phần kết hợp, thì bạn nên sử dụng phần lồng ghép trong Compose và đệm các thành phần kết hợp AndroidView
cho phù hợp.
Theo mặc định, mỗi ComposeView
sẽ sử dụng tất cả phần lồng ghép ở mức tiêu thụ WindowInsetsCompat
. Để thay đổi hành vi mặc định này, hãy đặt AbstractComposeView.consumeWindowInsets
thành false
.
Khả năng tương thích ngược khi gửi phần lồng ghép cho các khung hiển thị
Nếu ứng dụng của bạn chứa mã Lượt xem, bạn có thể cần xác nhận rằng phần lồng ghép được gửi đến các khung hiển thị cùng cấp trên những thiết bị chạy Android 10 (API cấp 29) trở xuống. Hãy xem hướng dẫn về các Khung hiển thị tràn viền để biết thêm thông tin.
Biểu tượng thanh hệ thống
Việc gọi enableEdgeToEdge
đảm bảo màu biểu tượng thanh hệ thống sẽ cập nhật khi giao diện thiết bị thay đổi.
Khi chuyển sang chế độ tràn viền, bạn có thể cần cập nhật màu biểu tượng thanh hệ thống theo cách thủ công để chúng tương phản với nền của ứng dụng. Ví dụ: để tạo biểu tượng thanh trạng thái sáng:
Kotlin
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = false
Java
WindowCompat.getInsetsController(window, window.getDecorView())
.setAppearanceLightStatusBars(false);
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-21 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-21 UTC."],[],[],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```"]]