SDK Android XR hiện đã có trong Bản dùng thử cho nhà phát triển. Chúng tôi rất mong nhận được ý kiến phản hồi của bạn! Hãy truy cập vào
trang hỗ trợ của chúng tôi để liên hệ.
Thêm không gian con vào ứng dụng
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.
Không gian phụ là một phân vùng của không gian 3D trong ứng dụng của bạn. Trong không gian phụ, bạn có thể đặt mô hình 3D, tạo bố cục 3D và thêm chiều sâu cho nội dung 2D. Không gian con chỉ được kết xuất khi bạn bật tính năng không gian. Trong Không gian nhà hoặc trên các thiết bị không phải XR, mọi mã trong không gian con đó sẽ bị bỏ qua.
Bạn có thể sử dụng các thành phần kết hợp không gian con như Volume
và SpatialPanel
để đặt mô hình 3D. Một số thành phần XR như Orbiter
hoặc SpatialDialog
là các thành phần kết hợp 2D tiêu chuẩn có thể được sử dụng ở bất kỳ đâu trong hệ phân cấp giao diện người dùng 2D, nhưng SubspaceComposable
phải được gọi trong không gian con của ứng dụng. Để thực hiện việc này, hãy sử dụng thành phần kết hợp ApplicationSubspace
hoặc thành phần kết hợp Subspace
.
Như tên gọi, thành phần kết hợp ApplicationSubspace
phải chứa tất cả nội dung không gian của ứng dụng. Thành phần kết hợp Subspace
là lựa chọn lý tưởng để lồng một phân vùng không gian 3D sâu hơn trong hệ phân cấp giao diện người dùng hiện có của ứng dụng.
Giống như mọi thành phần kết hợp khác, bạn có thể gọi Subspace
trực tiếp trong hệ phân cấp giao diện người dùng 2D. Tuy nhiên, điều quan trọng là bạn phải nhận biết được những tác động của vị trí trong hệ phân cấp mà bạn gọi Subspace
.
Giới thiệu về hệ phân cấp không gian con
Không gian con cấp cao nhất là không gian con ngoài cùng do ứng dụng gọi. Sử dụng thành phần kết hợp ApplicationSubspace
cho không gian con cấp cao nhất, nhưng nếu bạn chỉ sử dụng thành phần kết hợp Không gian con trong ứng dụng, thì thành phần kết hợp Subspace
ngoài cùng sẽ là không gian con cấp cao nhất. Theo mặc định, không gian con cấp cao nhất này được giới hạn bởi không gian đề xuất để xem ứng dụng và thường là nơi bạn đặt bố cục không gian của ứng dụng và SpatialPanel
. Nếu bạn cần thay đổi giới hạn của không gian con cấp cao nhất, hãy truyền nhiều VolumeConstraints
đến ApplicationSubspace
.
Tuy nhiên, nếu bạn lồng một không gian con khác bên trong hệ phân cấp giao diện người dùng 2D trong một bảng điều khiển nằm trong không gian con cấp cao nhất, thì không gian con được lồng đó sẽ hoạt động theo cách khác.
Không gian con lồng nhau có hai điểm khác biệt chính so với Subspace
cấp cao nhất:
- Các thành phần này tham gia vào bố cục 2D mà chúng được gọi. Điều này có nghĩa là chiều cao và chiều rộng của không gian con sẽ bị ràng buộc bởi chiều cao và chiều rộng của bố cục mẹ 2D.
- Các thành phần này hoạt động như thành phần con của thực thể mà chúng được gọi trong đó. Điều này có nghĩa là nếu bạn gọi một thành phần kết hợp
Subspace
được lồng bên trong một SpatialPanel
, thì không gian con đó là một phần tử con của SpatialPanel
được gọi trong đó.
Những hành vi này của không gian con lồng nhau cho phép các chức năng như:
- Di chuyển phần tử con bằng thực thể mẹ
- Bù vị trí của thành phần con bằng cách sử dụng độ dời
SubspaceModifier
- Trình bày một đối tượng 3D di chuột qua giao diện người dùng 2D và khớp với chiều cao và chiều rộng của không gian thích hợp trong bố cục 2D
Thêm không gian con vào ứng dụng
Mã ví dụ sau đây cho thấy cách thêm không gian con cấp cao nhất và lồng nhau vào ứng dụng:
setContent {
// This is a top-level subspace
ApplicationSubspace {
SpatialPanel {
MyComposable()
}
}
}
@Composable
private fun MyComposable() {
Row {
PrimaryPane()
SecondaryPane()
}
}
@Composable
private fun PrimaryPane() {
// This is a nested subspace, because PrimaryPane is in a SpatialPanel
// and that SpatialPanel is in a top-level Subspace
Subspace {
ObjectInAVolume(true)
}
}
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-23 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-23 UTC."],[],[],null,["A subspace is a partition of 3D space within your app where you can place 3D\nmodels, build 3D layouts, and add depth to otherwise 2D content. A subspace is\nrendered only when spatialization is enabled. In Home Space or on non-XR\ndevices, any code within that subspace is ignored.\n\nYou can use subspace composables such as [`Volume`](/reference/kotlin/androidx/xr/compose/subspace/package-summary#Volume(androidx.xr.compose.subspace.layout.SubspaceModifier,kotlin.Function1)) and [`SpatialPanel`](/reference/kotlin/androidx/xr/compose/subspace/package-summary#SpatialPanel(androidx.xr.compose.subspace.layout.SubspaceModifier,androidx.xr.compose.subspace.layout.SpatialShape,kotlin.Function0))\nfor placing 3D models. Some XR components such as [`Orbiter`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Orbiter(androidx.xr.compose.spatial.ContentEdge.Horizontal,androidx.compose.ui.unit.Dp,androidx.xr.compose.spatial.OrbiterOffsetType,androidx.compose.ui.Alignment.Horizontal,androidx.xr.compose.subspace.layout.SpatialShape,androidx.compose.ui.unit.Dp,kotlin.Boolean,kotlin.Function0)) or\n[`SpatialDialog`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#SpatialDialog(kotlin.Function0,androidx.xr.compose.spatial.SpatialDialogProperties,kotlin.Function0)) are standard 2D composables that can be used anywhere in\nyour 2D UI hierarchy, but [`SubspaceComposable`](/reference/kotlin/androidx/xr/compose/subspace/SubspaceComposable)s must be invoked in your\napp's subspace. To do this, use either the `ApplicationSubspace` composable or\nthe [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)) composable.\n\nAs the name suggests, the [`ApplicationSubspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#ApplicationSubspace(androidx.xr.compose.unit.VolumeConstraints,androidx.xr.compose.spatial.ConstraintsBehavior,kotlin.Function1)) composable should contain\nall of your app's spatialized content. The [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)) composable is ideal\nfor nesting a partition of 3D space deeper in your app's existing UI hierarchy.\n\nAs with any other composable, you can call [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)) directly in your 2D\nUI hierarchy. However, it's important to be aware of the implications of where\nin the hierarchy you invoke [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)).\n\nAbout subspace hierarchies\n\nThe top-level subspace is the outermost subspace invoked by your app. Use the\n[`ApplicationSubspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#ApplicationSubspace(androidx.xr.compose.unit.VolumeConstraints,androidx.xr.compose.spatial.ConstraintsBehavior,kotlin.Function1)) composable for your top-level subspace, but, if you\nonly use the Subspace composable in your app, the outermost `Subspace`\ncomposable will be your top-level subspace. By default, this top-level subspace\nis bounded by the recommended space for viewing an app, and it's typically where\nyou place your app's spatial layout and [`SpatialPanel`](/reference/kotlin/androidx/xr/compose/subspace/package-summary#SpatialPanel(androidx.xr.compose.subspace.layout.SubspaceModifier,androidx.xr.compose.subspace.layout.SpatialShape,kotlin.Function0)). If you need to\nchange the bounds of the top-level subspace, pass different\n[`VolumeConstraints`](/reference/kotlin/androidx/xr/compose/unit/VolumeConstraints) to your [`ApplicationSubspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#ApplicationSubspace(androidx.xr.compose.unit.VolumeConstraints,androidx.xr.compose.spatial.ConstraintsBehavior,kotlin.Function1)).\n\nHowever, if you nest another subspace inside of a 2D UI hierarchy in a panel\nthat's contained in the top-level subspace, that nested subspace behaves\ndifferently.\n\nNested subspaces have two key differences from top-level [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)):\n\n- They participate in the 2D layout in which they are invoked. This means that the height and width of the subspace will be constrained by the height and width of its 2D parent layout.\n- They behave as children of the entity they're invoked in. This means that, if you call a [`Subspace`](/reference/kotlin/androidx/xr/compose/spatial/package-summary#Subspace(kotlin.Function1)) composable nested inside of a [`SpatialPanel`](/reference/kotlin/androidx/xr/compose/subspace/package-summary#SpatialPanel(androidx.xr.compose.subspace.layout.SubspaceModifier,androidx.xr.compose.subspace.layout.SpatialShape,kotlin.Function0)), that subspace is a child of the [`SpatialPanel`](/reference/kotlin/androidx/xr/compose/subspace/package-summary#SpatialPanel(androidx.xr.compose.subspace.layout.SubspaceModifier,androidx.xr.compose.subspace.layout.SpatialShape,kotlin.Function0)) it's called in.\n\nThese behaviors of nested subspace enable capabilities such as:\n\n- Moving the child with the parent entity\n- Offsetting the location of the child using the offset [`SubspaceModifier`](/reference/kotlin/androidx/xr/compose/subspace/layout/SubspaceModifier)\n- Presenting a 3D object that hovers above your 2D UI and matches the height and width of the appropriate space in the 2D layout\n\nAdd a subspace to your app\n\nThe following code example shows how to add top-level and nested subspaces to\nyour app:\n\n\n```kotlin\nsetContent {\n // This is a top-level subspace\n ApplicationSubspace {\n SpatialPanel {\n MyComposable()\n }\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/xr/src/main/java/com/example/xr/compose/Subspace.kt#L34-L41\n```\n\n\u003cbr /\u003e\n\n\n```kotlin\n@Composable\nprivate fun MyComposable() {\n Row {\n PrimaryPane()\n SecondaryPane()\n }\n}\n\n@Composable\nprivate fun PrimaryPane() {\n // This is a nested subspace, because PrimaryPane is in a SpatialPanel\n // and that SpatialPanel is in a top-level Subspace\n Subspace {\n ObjectInAVolume(true)\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/xr/src/main/java/com/example/xr/compose/Subspace.kt#L47-L62\n```\n\n\u003cbr /\u003e"]]