如何将 DropdownMenu 显示为对话框

发布于 2025-01-15 23:15:57 字数 2187 浏览 3 评论 0原文

是否可以将 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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文