在Jetpack Compose中可以通过设置OverflowMenu来自动将操作项折叠到溢出菜单中。以下是示例代码:
@Composable
fun AppBar(
title: String,
actions: @Composable RowScope.() -> Unit
) {
TopAppBar(
title = { Text(text = title) },
actions = actions,
overflowMenu = {
DropdownMenu(
expanded = false,
onDismissRequest = { /*TODO*/ },
toggle = {
IconButton(onClick = { /*TODO*/ }) {
Icon(Icons.Filled.MoreVert, contentDescription = "Overflow menu")
}
}
) {
actions()
}
}
)
}
@Composable
fun ExampleScreen() {
Scaffold(
topBar = {
AppBar(
title = "Example Screen",
actions = {
IconButton(onClick = { /*TODO*/ }) {
Icon(Icons.Filled.Favorite, contentDescription = "Favorite")
}
IconButton(onClick = { /*TODO*/ }) {
Icon(Icons.Filled.Settings, contentDescription = "Settings")
}
}
)
}
) {
// Screen content
}
}
在这个示例中,我们将OverflowMenu设置为TopAppBar的属性,将操作折叠到溢出菜单中。当用户点击溢出菜单按钮时,DropdownMenu将显示菜单项。
上一篇:AndroidJetpackCompose预览中显示内容,但在手机中不显示。
下一篇:AndroidJetpackCompose中当子列生成更多的组合时,行的高度(IntrinsicSize.Min)不被拉伸。