我有代码:
@Composable
fun BottomNavigationBar(navController: NavController) {
val items = listOf(
BottomNavigationItem.One,
BottomNavigationItem.Two,
BottomNavigationItem.Three,
BottomNavigationItem.Four,
BottomNavigationItem.Five
)
BottomNavigation(
backgroundColor = colorResource(id = R.color.teal_700),
contentColor = Color.White
) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
items.forEach { item ->
BottomNavigationItem(
icon = { Icon(painterResource(id = item.icon), contentDescription = item.title) },
label = { Text(text = item.title) },
selectedContentColor = Color.White,
unselectedContentColor = Color.White.copy(0.4f),
alwaysShowLabel = true,
selected = currentRoute == item.route,
onClick = {
navController.navigate(item.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}
但是 contentColor 和 selectedContentColor 与 unselectedContentColor 不起作用。所选项目没有改变颜色(其他项目没有 unselectedContentColor)
我发现了错误(或者,也许这是正确的情况),但如果你有
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
属性 selectedContentColor 和 unselectedContentColor 将不起作用。 您必须使用以下导入:
import androidx.compose.material.Icon
import androidx.compose.material.Text
在
compose.material3
中,您必须使用导航栏和NavigationItems
。在 compose.material
(材质 2)中,您必须使用 BottomNavigationBar/Item。然后一切都按预期进行。