android compose Drawer抽屉 使用

android compose Drawer抽屉 使用

复制代码
/**
 * Author : wn
 * Email : maoning20080809@163.com
 * Date : 2026/1/4 20:54
 * Description : 抽屉Drawer
 */
class DrawerActivity : ComponentActivity(){

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

    @OptIn(ExperimentalMaterial3Api::class)
    @Preview
    @Composable
    private fun testDrawer(){
        val myDrawerState = rememberDrawerState(DrawerValue.Closed)
        val scope = rememberCoroutineScope()


        ModalNavigationDrawer(
            drawerState = myDrawerState,
            gesturesEnabled = true,
            drawerContent = {
                ModalDrawerSheet(modifier = Modifier.width(300.dp)) {
                    //抽屉头部
                    Spacer(modifier = Modifier.height(12.dp))

                    NavigationDrawerItem(
                        icon = { Icon(Icons.Default.AccountCircle,null) },
                        label = {Text(text = "用户名称")},
                        selected = false,
                        onClick = {}
                    )
                    
                    Spacer(modifier = Modifier.height(8.dp))

                    //分割线
                    Divider()

                    //导航项
                    NavigationDrawerItem(
                        icon = {Icon(Icons.Default.Home, null)},
                        label = {Text(text = "首页")},
                        selected = true,
                        onClick = {
                            scope.launch { myDrawerState.close() }
                        }
                    )

                    NavigationDrawerItem(
                        icon = {Icon(Icons.Default.Person, null)},
                        label = {Text(text = "个人资料")},
                        selected = false,
                        onClick = {
                            scope.launch { myDrawerState.close() }
                        }
                    )

                    NavigationDrawerItem(
                        icon = {Icon(Icons.Default.Settings, null)},
                        label = {Text(text = "设置")},
                        selected = false,
                        onClick = {
                            scope.launch { myDrawerState.close() }
                        }
                    )
                }
            }
        ) {
            Scaffold(
                topBar = {
                    TopAppBar(title = {Text(text = "我的应用")},
                        navigationIcon = {
                            IconButton(
                                onClick = {
                                    scope.launch { myDrawerState.open() }
                                }
                            ) {
                                Icon(Icons.Default.Menu, "打开抽屉")
                            }
                        }
                    )
                }
            ) {  paddingValues ->
                Box(
                    modifier = Modifier.fillMaxSize().padding(paddingValues),
                    contentAlignment = Alignment.Center
                ){
                    Text(text = "主内容区域")
                }

            }
        }
    }
}
相关推荐
峥嵘life1 天前
Android 17 具体代码新特性总结
android·开发语言·网络
王者鳜錸1 天前
Android 接入 AIKit 轻量版语音合成实战
android·交互·多语种·离线合成·多种发音人
『 时光荏苒 』1 天前
SQL 中常用的查询条件分类汇总,每个都配有“大白话注释”和示例,方便你对照学习
android·java·开发语言
智购科技自动贩卖机1 天前
自动售货机SaaS多租户数据库实战:分库分表与租户隔离~YH
android·java·linux·开发语言·javascript·网络
遥不可及zzz1 天前
Android 接入 Helpshift SDK 详细教程(Java 实战):客服聊天、FAQ、用户登录及消息推送
android·helpshift
Digitally1 天前
通过蓝牙将文件从 iPhone 传输到 Android
android
程序猿老A1 天前
轻量应用服务器怎么配置mysql
android·mysql·adb
weixin_394758032 天前
CRMEB Pro 优惠券:下单怎么核销,取消/退款怎么返券
android
alexhilton2 天前
Android 17 内存预算成为必修课
android·kotlin·android jetpack
AI2中文网2 天前
App Inventor 对接 OneNet:手机收数据
android·鸿蒙·app inventor