Android XR SDK 現已在開發人員預覽版中推出。敬請提供意見回饋!請前往
支援頁面與我們聯絡。
在應用程式中新增子區
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
子空間是應用程式內 3D 空間的分區,可在其中放置 3D 模型、建構 3D 版面配置,以及為其他 2D 內容加上深度。只有在啟用空間化功能時,系統才會算繪子空間。在 Home Space 或非 XR 裝置上,系統會忽略該子空間中的任何程式碼。
您可以使用子區域可組合函式 (例如 Volume
和 SpatialPanel
) 來放置 3D 模型。部分 XR 元件 (例如 Orbiter
或 SpatialDialog
) 是標準的 2D 可組合函式,可用於 2D UI 階層中的任何位置,但 SubspaceComposable
必須在應用程式的子區域中叫用。如要執行這項操作,請使用 ApplicationSubspace
可組合函式或 Subspace
可組合函式。
如名稱所示,ApplicationSubspace
可組合函式應包含應用程式的所有空間化內容。Subspace
可組合項非常適合在應用程式現有的 UI 階層中,將 3D 空間的分區巢狀結構更深入。
如同其他可組合項,您可以在 2D UI 階層中直接呼叫 Subspace
。不過,請務必瞭解在階層中叫用 Subspace
的影響。
關於子空間階層
頂層子空間是應用程式叫用的最外層子空間。請為頂層子空間使用 ApplicationSubspace
可組合項,但如果您只在應用程式中使用子空間可組合項,最外層的 Subspace
可組合項將會是頂層子空間。根據預設,這個頂層子空間會受限於觀看應用程式的建議空間,通常是放置應用程式空間版面配置和 SpatialPanel
的位置。如果您需要變更頂層子空間的邊界,請將不同的 VolumeConstraints
傳遞至 ApplicationSubspace
。
不過,如果您在包含頂層子區塊的面板中,在 2D UI 階層中巢狀結構另一個子區塊,該巢狀結構子區塊的行為會有所不同。
巢狀子空間與頂層 Subspace
有兩個主要差異:
巢狀子空間的這些行為可啟用以下功能:
- 使用父項實體移動子項
- 使用偏移量
SubspaceModifier
偏移子項的位置
- 呈現 3D 物件,讓其懸停在 2D UI 上方,並與 2D 版面配置中適當空間的高度和寬度相符
在應用程式中新增子空間
以下程式碼範例說明如何在應用程式中新增頂層和巢狀子集:
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)
}
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-28 (世界標準時間)。
[null,null,["上次更新時間:2025-08-28 (世界標準時間)。"],[],[],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"]]