Jetpack Compose——DropdownMenu(下拉菜单)的简单使用
程序员文章站
2022-05-14 20:23:25
...
基于compose_version=1.1.0-beta01版本 参数如下:
DropdownMenu(
expanded: Boolean,//菜单栏状态
onDismissRequest: () -> Unit,//菜单栏被关闭回调
modifier: Modifier = Modifier,//布局修饰
offset: DpOffset = DpOffset(0.dp, 0.dp),//要添加到菜单位置的偏移量
properties: PopupProperties = PopupProperties(focusable = true),//自定义的弹出框
content: @Composable ColumnScope.() -> Unit//内容
)
简单使用:
@Composable
fun DropDownTest() {
val context = LocalContext.current
var expanded = remember {
mutableStateOf(false)
}
val items = listOf("A", "B", "C", "D", "E")
Box(
Modifier
.fillMaxSize()
.padding(10.dp)
.wrapContentSize(Alignment.TopStart)
) {
TextButton(
onClick = {
expanded.value = true
},
modifier = Modifier.background(Color.LightGray)
) {
Text(text = "下拉菜单")
}
DropdownMenu(expanded = expanded.value, onDismissRequest = { expanded.value = false }) {
items.forEachIndexed { index, s ->
DropdownMenuItem(onClick = {
expanded.value = false
Toast.makeText(context, s, Toast.LENGTH_SHORT).show()
}) {
Text(text = s)
}
}
}
}
}
效果如图: