我想将汉堡包图标添加到应用栏,然后使用该图标打开/关闭抽屉。
我将如何实现?
Scaffold(
drawerShape = RoundedCornerShape(topRight = 10.dp, bottomRight = 10.dp),
drawerElevation = 5.dp,
drawerContent = {
// Drawer
},
topBar = {
TopAppBar(
navigationIcon = {
Icon(
Icons.Default.Menu,
modifier = Modifier.clickable(onClick = {
// Open drawer => How?
})
)
},
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(bottomLeft = 10.dp, bottomRight = 10.dp)),
title = { Text(text = "Hello") }
)
},
) {}
答案 0 :(得分:5)
使用 rememberScaffoldState()
修改抽屉状态。
val state = rememberScaffoldState()
Scaffold
Scaffold(
scaffoldState = state,
// ...
)
在state.drawerState.open()
中使用state.drawerState.close()
或onClick
打开/关闭抽屉。
在navigationIcon
中为TopAppBar
创建图标:
val state = rememberScaffoldState()
Scaffold(
scaffoldState = state,
topBar = {
TopAppBar(
title = { Text(text = "AppBar") },
navigationIcon = {
Icon(
Icons.Default.Menu,
modifier = Modifier.clickable(onClick = {
state.drawerState.open() // or toggle
})
)
}
)
},
drawerShape = RoundedCornerShape(topRight = 10.dp, bottomRight = 10.dp),
drawerContent = {
Text(text = "Drawer")
}
) {
// Scaffold body
}
答案 1 :(得分:3)