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")
}
相关推荐
浅影歌年43 分钟前
Android和h5页面相互传参
android
用户69371750013841 小时前
搞懂 Kotlin 软关键字与硬关键字:灵活命名与语法陷阱全解析
android
下位子2 小时前
『OpenGL学习滤镜相机』- Day2: 渲染第一个三角形
android·opengl
风语者日志2 小时前
[LitCTF 2023]这是什么?SQL !注一下 !
android·数据库·sql
2501_915921433 小时前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹3 小时前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友
游戏开发爱好者83 小时前
iOS 应用逆向对抗手段,多工具组合实战(iOS 逆向防护/IPA 混淆/无源码加固/Ipa Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
虚伪的空想家4 小时前
ip网段扫描机器shell脚本
android·linux·网络协议·tcp/ip·shell·脚本·network
generallizhong4 小时前
android TAB切换
android·gitee
00后程序员张4 小时前
iOS 文件管理与导出实战,多工具协同打造高效数据访问与调试体系
android·macos·ios·小程序·uni-app·cocoa·iphone