效果图:
依赖引入:
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")
}