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")
}
相关推荐
erqi3 小时前
Compose你入门吧
android
q***76563 小时前
MySQL 中如何进行 SQL 调优
android·sql·mysql
zhanglinping6193 小时前
MySQL——内置函数
android·数据库·mysql
m***78743 小时前
mysql之字符串函数
android·数据库·mysql
w***71104 小时前
MySQL 事务的操作和四大特性
android·数据库·mysql
松叶似针6 小时前
Flutter三方库适配OpenHarmony【secure_application】— Android 端 FLAG_SECURE 实现分析
android·flutter
cjl_8520087 小时前
[MySQL] MySQL复合查询(多表查询、子查询)
android·mysql·adb
stevenzqzq8 小时前
Barrier的用法
android
城东米粉儿9 小时前
Android 窗口添加/删除流程 笔记
android
城东米粉儿9 小时前
Android Window 窗口动画原理
android