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")
}
相关推荐
赏金术士3 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
问心无愧05135 小时前
ctf show web 入门42
android·前端·android studio
没什么本事6 小时前
关于C# panel 添加lable问题 -- 明确X和Y 位置错误
android·java·c#
REDcker9 小时前
Android HWASan 详解:硬件标记原理、Clang 启用与排障实践
android·linux·debug·编译·clang·asan·hwasan
2501_9159090610 小时前
全面解析前端开发中常用的浏览器调试工具及其使用场景
android·ios·小程序·https·uni-app·iphone·webview
angerdream10 小时前
Android手把手编写儿童手机远程监控App之SQLite详解2
android
-SOLO-10 小时前
Python 爬取小红书 文章标题和内容 仅供学习
android·python·学习
ooseabiscuit10 小时前
Laravel5
android·php·laravel
科技道人11 小时前
Android 禁止使用ipv6 测试
android·禁用ipv6
AlexMaybeBot11 小时前
巧用 OpenClaw 为 Android 开发电脑瘦身
android·github·ai编程