kotlin,Jetpack Compose使用Scaffold布局,包含底部导航栏

复制代码
package com.example.dibudaohang

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import com.example.dibudaohang.ui.theme.DibudaohangTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            DibudaohangTheme {
                BottomNavigationExample()
            }
        }
    }
}

@Composable
fun BottomNavigationExample() {
    // 使用remember保存选中的项,初始值为0
    var selectedItem by remember { mutableStateOf(0) }
    // 定义底部导航栏的项
    val items = listOf("Home", "Search", "Profile")

    // 使用Scaffold布局,包含底部导航栏
    Scaffold(
        bottomBar = {
            // 创建底部导航栏
            NavigationBar {
                // 遍历items列表,为每个项创建一个NavigationBarItem
                items.forEachIndexed { index, item ->
                    NavigationBarItem(
                        // 根据index选择不同的图标
                        icon = {
                            when (index) {
                                0 -> Icon(Icons.Default.Home, contentDescription = item)
                                1 -> Icon(Icons.Default.Search, contentDescription = item)
                                2 -> Icon(Icons.Default.Person, contentDescription = item)
                            }
                        },
                        // 显示项的标签
                        label = { Text(item) },
                        // 判断当前项是否被选中
                        selected = selectedItem == index,
                        // 点击事件,更新选中的项
                        onClick = { selectedItem = index }
                    )
                }
            }
        }
    ) { innerPadding ->
        // 根据选中的项显示不同的内容
        when (selectedItem) {
            0 -> Text("Home Screen", modifier = Modifier.padding(innerPadding))
            1 -> Text("Search Screen", modifier = Modifier.padding(innerPadding))
            2 -> Text("Profile Screen", modifier = Modifier.padding(innerPadding))
        }
    }
}

注意:在最新的 Jetpack Compose 中,BottomNavigation 已经被 NavigationBar 取代。NavigationBar 是 androidx.compose.material3 包中的新组件,用于实现底部导航栏。
相关推荐
Zender Han14 小时前
Flutter 新版 Google Sign-In 插件完整解析(含示例讲解)
android·flutter·ios·web
来来走走17 小时前
Android开发(Kotlin) LiveData的基本了解
android·开发语言·kotlin
。puppy18 小时前
MySQL 远程登录实验:通过 IP 地址跨机器连接实战指南
android·adb
dongdeaiziji18 小时前
深入理解 Kotlin 中的构造方法
android·kotlin
风起云涌~19 小时前
【Android】浅谈Navigation
android
游戏开发爱好者819 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
QuantumLeap丶21 小时前
《Flutter全栈开发实战指南:从零到高级》- 18 -自定义绘制与画布
android·flutter·ios
.豆鲨包21 小时前
【Android】 View事件分发机制源码分析
android·java
花落归零1 天前
Android 小组件AppWidgetProvider的使用
android
弥巷1 天前
【Android】常见滑动冲突场景及解决方案
android·java