为方便使用,许多内置的 Material 3 可组合项 (androidx.compose.material3
) 会根据可组合项在应用中根据 Material 规范的放置方式,自行处理边衬区。
处理边衬的可组合项
下面列出了自动处理边衬区的 Material 组件。
应用栏
TopAppBar
/SmallTopAppBar
/CenterAlignedTopAppBar
/MediumTopAppBar
/LargeTopAppBar
:将系统栏的顶部和水平边应用为内边距,因为它用于窗口顶部。BottomAppBar
:将系统栏的底部和水平侧应用为内边距。
内容容器
ModalDrawerSheet
/DismissibleDrawerSheet
/PermanentDrawerSheet
(模态抽屉式导航栏中的内容):将垂直和起始内嵌应用于内容。ModalBottomSheet
:应用底部内边距。NavigationBar
:应用底部和水平内边距。NavigationRail
:应用垂直和起始内边距。
Scaffold
默认情况下,Scaffold
会将内边距作为参数 paddingValues
提供给您使用。Scaffold
不会将边衬应用于内容;此责任由您承担。
例如,如需在 Scaffold
内使用 LazyColumn
来使用这些内嵌,请执行以下操作:
Scaffold { innerPadding -> // innerPadding contains inset information for you to use and apply LazyColumn( // consume insets as scaffold doesn't do it by default modifier = Modifier.consumeWindowInsets(innerPadding), contentPadding = innerPadding ) { // .. } }
以下视频展示了 Scaffold
中的 LazyColumn
,其中边到边显示屏处于停用和启用状态:
替换默认内边距
您可以更改传递给可组合项的 windowInsets
形参,以配置可组合项的行为。此参数可以是其他类型的窗口内边距,以替代要应用的内边距;也可以通过传递空实例 WindowInsets(0, 0, 0, 0)
来停用。
例如,如需停用 LargeTopAppBar
上的内边处理,请将 windowInsets
参数设置为空实例:
LargeTopAppBar( windowInsets = WindowInsets(0, 0, 0, 0), title = { Text("Hi") } )