compose——侧边栏

效果图:

依赖引入:

Kotlin 复制代码
implementation 'com.google.android.material:material:1.5.0'

代码如下:

Kotlin 复制代码
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposedrawerTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    MainPage()
                }
            }
        }
    }
}

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainPage() {
    val scaffoldState = rememberScaffoldState()
    val coroutineScope = rememberCoroutineScope()// 定义协程作用域

    Scaffold(
        topBar = {
            TopAppBar(
                navigationIcon = {
                    IconButton(
                        onClick = {
                            coroutineScope.launch {
                                scaffoldState.drawerState.open()
                            }
                        }
                    ) {
                        Icon(Icons.Filled.Menu, null)
                    }
                },
                title = {
                    Text("首页")
                },
                actions = {
                    IconButton(
                        onClick = {}
                    ) {
                        Icon(Icons.Filled.Share, null)
                    }
                    IconButton(
                        onClick = {}
                    ) {
                        Icon(Icons.Filled.Settings, null)
                    }
                },
            )
        },
        drawerContent = { DrawerContent() },
        drawerGesturesEnabled = true,
        scaffoldState = scaffoldState,
    ) {

    }
}

@Composable
fun DrawerContent() {
    Text(text = "DrawerContent")
}
相关推荐
yuanhello几秒前
【Android】Android的键值对存储方案对比
android·java·android studio
Ditglu.1 分钟前
CentOS7 MySQL5.7 主从复制最终版搭建流程(避坑完整版)
android·adb
恋猫de小郭8 分钟前
Android Studio Otter 2 Feature 发布,最值得更新的 Android Studio
android·前端·flutter
走在路上的菜鸟30 分钟前
Android学Dart学习笔记第十二节 函数
android·笔记·学习·flutter
没有了遇见30 分钟前
Android + Google Play:老项目适配实战指南
android·google
怀君40 分钟前
Uniapp——开发Android插件教程
android·uni-app
Lei活在当下1 小时前
【Perfetto从入门到精通】1. 初识 Perfetto
android·性能优化·架构
用户41659673693551 小时前
深度解析 Android 权限机制:从清单注册到 Android 14 适配实战
android
Nerve3 小时前
GalleryPicker:一个基于 Android 官方 Photo Picker API 封装的现代图片/视频选择库
android
伐尘4 小时前
【MySQL】MySQL 有效利用 profile 分析 SQL 语句的执行过程
android·sql·mysql