如何移除Jetpack Compose中NavigationBarItem的椭圆形指示器?
要彻底移除选中项的椭圆形指示器,仅修改indicatorColor有时会因Material组件默认绘制逻辑失效,最可靠的方式是直接禁用指示器的绘制:
在NavigationBarItem中添加indicator = {}参数,让它不渲染任何指示器内容,具体修改如下:
修改后的AddItem代码
@Composable fun RowScope.AddItem( screen: BottomNavItems, currentDestination: NavDestination?, navController: NavHostController ) { NavigationBarItem( selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true, onClick = { if (currentDestination?.hierarchy?.any { it.route == screen.route } == false) { navController.navigate(screen.route) { popUpTo(navController.graph.findStartDestination().id) launchSingleTop = true } } }, alwaysShowLabel = true, label = {}, icon = { Column( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Icon( painter = painterResource(id = screen.icon), contentDescription = screen.name, modifier = Modifier.size(30.dp) ) Text(text = screen.name, style = MaterialTheme.typography.labelSmall) } }, colors = NavigationBarItemDefaults.colors( selectedIconColor = Color.White, unselectedIconColor = Color.Gray, indicatorColor = Color.Transparent // 这行可保留也可删除,已通过indicator参数禁用绘制 ), indicator = {} // 关键:添加此行彻底禁用指示器绘制 ) }
补充说明
你之前修改indicatorColor无效,是因为NavigationBarItem默认的indicator组件(由NavigationBarItemDefaults.Indicator提供)会基于主题 elevation 或其他属性绘制背景,即便颜色设为透明,组件本身的绘制逻辑仍会执行。直接将indicator设为空Composable,就能完全取消指示器的渲染。
另外你在BottomBar中定义的colors变量未传递给AddItem里的NavigationBarItem,所以那部分设置实际未生效,所有颜色控制都是在AddItem中完成的,这点也可以留意。
内容的提问来源于stack exchange,提问作者dum
相关产品推荐
相关产品推荐

