如何在启动页(Splash Screen)中隐藏底部导航栏
在Android Jetpack Compose启动页隐藏底部导航栏的解决方案
你可以通过路由判断控制底部导航栏的显示逻辑,仅在非启动页时渲染BottomAppBar,就能实现启动页隐藏底部导航栏的效果。修正后的完整代码如下:
Scaffold( bottomBar = { // 仅当前路由不是启动页时,显示底部导航栏 if (currentRoute(navController) != Screen.SplashScreen.route) { BottomAppBar( modifier = modifier .height(80.dp) .padding(5.dp) .clip(shape = RoundedCornerShape(60.dp)), containerColor = Color.Gray, tonalElevation = 5.dp ) { items.forEach { item -> val selected = item.route == backStackEntry.value?.destination?.route NavigationBarItem( selected = item.route == navController.currentDestination?.route, onClick = { onItemClick(item) }, icon = { Column(horizontalAlignment = Alignment.CenterHorizontally) { if (item.badgeCount > 0) { BadgedBox(badge = { Text(text = item.badgeCount.toString()) }) { Icon(imageVector = item.icon, contentDescription = item.name) } } else { Icon(imageVector = item.icon, contentDescription = item.name) } if (selected) { Text(text = item.name, textAlign = TextAlign.Center, fontSize = 10.sp) } } }, colors = NavigationBarItemDefaults.colors( selectedIconColor = Color.Green, unselectedIconColor = Color.Red, selectedTextColor = Color.Green, indicatorColor = Color.DarkGray ) ) } } } } ) { innerPadding -> // 页面内容区域,使用innerPadding避免被底部导航栏遮挡 }
关键说明
- 核心逻辑是通过
currentRoute(navController)获取当前活跃页面的路由,与启动页Screen.SplashScreen.route做对比 - 当路由不匹配启动页时,才会执行
BottomAppBar的渲染代码,启动页自然不会显示底部导航栏 - 注意保留
Scaffold的content参数中的innerPadding,避免非启动页的内容被底部导航栏遮挡
内容的提问来源于stack exchange,提问作者Anurenj Sudheer
相关产品推荐
相关产品推荐

