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")
}
相关推荐
iblade23 分钟前
Android CLI And Skills 3x faster
android
阿巴斯甜2 小时前
SharedUnPeekLiveData和UnPeekBus的区别:
android
阿巴斯甜2 小时前
UnPeek-LiveData的使用:
android
我就是马云飞2 小时前
我废了!大厂10年的我面了20家公司,面试官让我回去等通知!
android·前端·程序员
limuyang23 小时前
在 Android 上用上原生的 xxHash,性能直接拉满
android
Fate_I_C4 小时前
ViewModel 的生命周期与数据保持
android·kotlin
凛_Lin~~4 小时前
安卓实现textview跑马灯效果
android·java
Fate_I_C5 小时前
Kotlin函数一
android·开发语言·kotlin
我讲个笑话你可别哭啊5 小时前
Android Studio无线调试连接安卓设备
android·ide·android studio
pengyu5 小时前
【Kotlin 协程修仙录 · 炼气境 · 初阶】 | 感受天地灵气,写出第一个挂起函数
android·kotlin