Problem
在某些文档中,Composable androidx.compose.material.NavigationRail
具有可选的"居中"对齐方式.我无法用Jetpack Compose实现这一点.
"旧"XML属性是app:menuGravity="center"
according to this tutorial.
Things I tried without success
我试图将NavigationRail(实际小部件的简单包装)放入一个带有对齐设置的框中:
Box(contentAlignment = Alignment.Center) {
NavigationRail(
selectedItem = selectedItem,
onSelectItem = { selectedItem = it }
)
}
并将其直接放入原始组合中:
NavigationRail(
modifier = modifier.width(80.dp),
backgroundColor = MaterialTheme.colors.surface
) {
Box(contentAlignment = Alignment.Center) {
for (item in NavigationItem.values()) {
NavigationRailItem(
selected = selectedItem == item,
onClick = { onSelectItem.invoke(item) },
icon = { Icon(item.icon, item.title) },
alwaysShowLabel = true,
selectedContentColor = MaterialTheme.colors.primaryVariant,
unselectedContentColor = MaterialTheme.colors.primary,
label = { Text(item.title, modifier = Modifier.padding(top = 8.dp)) }
)
}
}
}
Screenhot