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)
    }

}
相关推荐
用户607097591490622 分钟前
AOSP 14 OTA 升级指导(三):三条升级链路——Java API、自研 HAL 与 TR069
android
前端小D1 小时前
React Native Android 项目搭建
android·react native·react.js
恋猫de小郭2 小时前
感人,Flutter 居然有了一个超实用提案,期待落地
android·前端·flutter
CS_Zero3 小时前
安卓开发:Linux的android工具命令
android·linux·运维
fellow994 小时前
DSH Desktop - Android Edition
android·agent·deepseek·harness·dsh
Carson带你学Android4 小时前
Android Studio Rabbit 2 发布:除了Gemini,40+ AI Agent 正式入驻!
android·ai编程
三少爷的鞋16 小时前
Java 线程自上而下,Kotlin 协程自下而上
android
好的佩奇18 小时前
Dart 之增强枚举
android·flutter·dart
梦影_18 小时前
MySQL进阶教程(五)—— SQL调优(下)
android·大数据·数据库·后端·sql·mysql·database
好的佩奇21 小时前
蓝牙之无线电与通信距离
android·flutter·蓝牙