AndroidJetpackCompose底部导航栏问题
创始人
2024-10-08 10:31:23
0
  1. 在 build.gradle 文件中添加依赖项:
dependencies {
    implementation "androidx.navigation:navigation-compose:$nav_version"
}
  1. 在 Composable 函数中设置导航栏:
@Composable
fun BottomNavigation() {
    val navController = rememberNavController()
    BottomNavigation {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route

        BottomNavigationItem(
            icon = { Icon(Icons.Filled.Home, contentDescription = "Home") },
            label = { Text("Home") },
            selected = currentRoute == "home",
            onClick = {
                navController.navigate("home") {
                    popUpTo(navController.graph.startDestinationId)
                    launchSingleTop = true
                }
            }
        )

        BottomNavigationItem(
            icon = { Icon(Icons.Filled.Favorite, contentDescription = "Favorite") },
            label = { Text("Favorite") },
            selected = currentRoute == "favorite",
            onClick = {
                navController.navigate("favorite") {
                    popUpTo(navController.graph.startDestinationId)
                    launchSingleTop = true
                }
            }
        )

        BottomNavigationItem(
            icon = { Icon(Icons.Filled.Settings, contentDescription = "Settings") },
            label = { Text("Settings") },
            selected = currentRoute == "settings",
            onClick = {
                navController.navigate("settings") {
                    popUpTo(navController.graph.startDestinationId)
                    launchSingleTop = true
                }
            }
        )
    }
}
  1. 在 NavHost 中设置底部导航栏:
NavHost(
    navController = navController,
    startDestination = "home"
) {
    composable("home") { HomeScreen() }
    composable("favorite") { FavoriteScreen() }
    composable("settings") { SettingsScreen() }
}

BottomNavigation()

相关内容

热门资讯

透视总结!poker worl... 透视总结!poker world辅助(透视)都是有挂(详细辅助2025新版);1、实时开挂更新:用户...
一分钟揭秘!约局吧德州有挂吗,... 一分钟揭秘!约局吧德州有挂吗,拱趴大菠萝万能挂图解,详细教程(有挂软件);致您一封信;亲爱拱趴大菠萝...
透视了解!wepoker透视脚... 透视了解!wepoker透视脚本(透视)都是存在有挂(详细辅助透牌教程)1、用户打开应用后不用登录就...
重大通报!aa poker辅助... 这是一款非常优秀的aa poker辅助 ia辅助检测软件,能够让你了解到aa poker辅助中牌率当...
查到实测辅助!hhpoker是... 您好,菠萝辅助器免费版的功能介绍这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视了解!wepoker有没有... 透视了解!wepoker有没有机器人(透视)果然存在有挂(详细辅助软件教程);1、系统规律教程、辅助...
透视ai代打!hhpoker免... 透视ai代打!hhpoker免费透视脚本(透视)一贯有挂(详细辅助规律教程)hhpoker免费透视脚...
重要通知!wepoker挂,h... 重要通知!wepoker挂,hh poker辅助有用吗,专业教程(有挂技巧);最新版2024是一款经...
透视线上!wepoker轻量版... 透视线上!wepoker轻量版辅助(透视)竟然真的有挂(详细辅助详细教程)1、任何ai辅助神器的玩家...
推荐十款!pokemmo辅助官... 推荐十款!pokemmo辅助官网,wepoker钻石怎么看底牌,力荐教程(有挂教程);建议优先通过w...