android compose BottomNavigation(底部) 使用

android compose BottomNavigation(底部) 使用

复制代码
/**
 * Author : wn
 * Email : maoning20080809@163.com
 * Date : 2026/1/3 13:06
 * Description : BottomNavigation 底部导航栏
 */
class BottomNavigationActivity : ComponentActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            testBottomNavigation()
        }
    }

    @Preview
    @Composable
    private fun testBottomNavigation(){
        //要添加Composable,导入:import androidx.compose.runtime.getValue和import androidx.compose.runtime.setValue
        var selectedItem by remember { mutableStateOf(0) }
        val items = listOf<String>("Home", "Search", "Profile")
        val icons = listOf(Icons.Default.Home, Icons.Default.Search, Icons.Default.Person)

        Scaffold(bottomBar = {
            NavigationBar() {
                items.forEachIndexed {index, item ->
                    NavigationBarItem(
                        icon = {Icon(icons[index], contentDescription = item)},
                        label = {Text(item)},
                        selected = selectedItem == index,
                        onClick = {selectedItem = index }
                    )
                }
            }
        }) {
            when(selectedItem){
                0 -> testHome()
                1 -> testSearch()
                2 -> testProfile()
            }
        }
    }

    @Composable
    private fun testHome(){
        Text(text = "Home 页面")
    }

    @Composable
    private fun testSearch(){
        Text(text = "Search 页面", fontSize = 30.sp, color = Color.Red)
    }

    @Composable
    private fun testProfile(){
        Text(text = "Profile 页面", fontSize = 30.sp, color = Color.Blue)
    }

}
相关推荐
好的佩奇2 小时前
Dart 之元数据与注解
android·flutter·dart
三少爷的鞋3 小时前
很多开发者把物理上的分层,当成了架构
android
FungLeo4 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白5 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快8 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks1 天前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui1 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android2 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin