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")
}
相关推荐
帅次6 分钟前
Flutter Container 组件详解
android·flutter·ios·小程序·kotlin·iphone·xcode
ThMoonAdSixPence28 分钟前
在Android Studio中复现AOSP原生GL2JNI图形渲染应用
android·android studio
用户20187928316730 分钟前
Android 系统稳定性侦探事务所:超时与崩溃案件调查
android
用户20187928316731 分钟前
Android 侦探事务所:Throwable 侦探的案件追踪手册
android
用户20187928316731 分钟前
Android 日志邮局:logd 的信件处理全流程
android
用户20187928316731 分钟前
Android 城市事件记录局:DropBoxManagerService 的工作日常
android
头发够用的程序员1 小时前
小米玄戒O1架构深度解析(二):多核任务调度策略详解
android·linux·arm开发·智能手机·架构·手机
消失的旧时光-19432 小时前
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
android·gitee
用户2018792831672 小时前
Android 内存管家 LMK 的一天:从贴标签到赶进程的故事
android
PenguinLetsGo2 小时前
某加固软件 odex 方式运行必现闪退
android