如何修改Material3 BottomNavigationMenu选中图标背景色?
全局主题修改(推荐)
Material3为BottomNavigation提供了专属主题属性,修改后全局生效:
- 打开
res/values/themes.xml(或styles.xml),找到你的Material3主题(如Theme.YourAppName) - 添加如下属性,替换为你需要的颜色值或颜色资源:
<item name="bottomNavigationBarSelectedItemContainerColor">@color/your_target_color</item>
单独组件自定义样式
如果只想给特定BottomNavigationView设置样式,不影响全局:
- 在
res/values/styles.xml中创建自定义样式:
<style name="CustomBottomNavStyle" parent="Widget.Material3.BottomNavigationView"> <item name="selectedItemContainerColor">@color/your_target_color</item> </style>
- 在布局文件的BottomNavigationView标签中引用该样式:
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/CustomBottomNavStyle" app:menu="@menu/your_bottom_nav_menu" />
Jetpack Compose场景适配
如果使用Compose的BottomNavigation,可通过indicator参数自定义高亮背景:
BottomNavigation( containerColor = MaterialTheme.colorScheme.background ) { items.forEach { item -> BottomNavigationItem( icon = { Icon(item.icon, contentDescription = item.label) }, label = { Text(item.label) }, selected = currentRoute == item.route, onClick = { navigateTo(item.route) }, indicator = { Box( modifier = Modifier .fillMaxWidth() .height(4.dp) .background(color = Color.Green) // 此处设置选中项高亮背景色 ) } ) } }
内容的提问来源于stack exchange,提问作者Shaun Lennon
相关产品推荐
相关产品推荐

