如何将 DropdownMenu 显示为对话框
是否可以将 DropdownMenu
中的项目显示为 Dialog
而不是传统的 Spinner
?我注意到前者更擅长处理和展示更多的物品。
预期结果
当前代码
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyAppTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
VerticalScrollView()
}
}
}
}
}
@Composable
fun VerticalScrollView() {
val list = listOf(
stringResource(R.string.item_1),
stringResource(R.string.item_2),
stringResource(R.string.item_3))
val expanded = remember{ mutableStateOf(false)}
val currentValue = remember{ mutableStateOf(listP[0])}
Column (
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
Row {
Text(stringResource(R.string.property_type_asterisk),
style = MaterialTheme.typography.subtitle1,
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 0.dp))
}
Row(modifier = Modifier.clickable {
expandedP.value = !expandedP.value
}) {
Text(text = currentValueP.value)
Icon(imageVector = Icons.Filled.ArrowDropDown, contentDescription = null)
DropdownMenu(expanded = expandedP.value, onDismissRequest = {
expandedP.value = false
}) {
listP.forEach {
DropdownMenuItem(onClick = {
currentValueP.value = it
expandedP.value = false
}) {
Text(text = it)
}
}
}
}
}
}
}
Is it possible to show items in a DropdownMenu
as a Dialog
rather than a traditional Spinner
? I noticed that the former is much better at handling and display a bigger set of items.
Expected result
Current code
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyAppTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
VerticalScrollView()
}
}
}
}
}
@Composable
fun VerticalScrollView() {
val list = listOf(
stringResource(R.string.item_1),
stringResource(R.string.item_2),
stringResource(R.string.item_3))
val expanded = remember{ mutableStateOf(false)}
val currentValue = remember{ mutableStateOf(listP[0])}
Column (
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
Row {
Text(stringResource(R.string.property_type_asterisk),
style = MaterialTheme.typography.subtitle1,
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 0.dp))
}
Row(modifier = Modifier.clickable {
expandedP.value = !expandedP.value
}) {
Text(text = currentValueP.value)
Icon(imageVector = Icons.Filled.ArrowDropDown, contentDescription = null)
DropdownMenu(expanded = expandedP.value, onDismissRequest = {
expandedP.value = false
}) {
listP.forEach {
DropdownMenuItem(onClick = {
currentValueP.value = it
expandedP.value = false
}) {
Text(text = it)
}
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论