gpt4 book ai didi

android - Jetpack compose 的公开下拉菜单

转载 作者:行者123 更新时间:2023-12-04 11:17:26 25 4
gpt4 key购买 nike

我想知道是否有针对 jetpack compose 的 Exposed 下拉菜单的解决方案?
我在 jetpack compose 中找不到该组件的合适解决方案。有什么帮助吗?
Drop-down

最佳答案

版本 1.1.0-alpha06 介绍了ExposedDropdownMenu的实现基于 ExposedDropdownMenuBox TextFieldDropdownMenu里面。
就像是:

    val options = listOf("Option 1", "Option 2", "Option 3", "Option 4", "Option 5")
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[0]) }

ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
readOnly = true,
value = selectedOptionText,
onValueChange = { },
label = { Text("Label") },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
options.forEach { selectionOption ->
DropdownMenuItem(
onClick = {
selectedOptionText = selectionOption
expanded = false
}
) {
Text(text = selectionOption)
}
}
}
}
enter image description here
使用版本 1.0.x没有内置组件。
您可以使用 OutlinedTextField + DropdownMenu .
这只是一个基本的(非常基本的)实现:
var expanded by remember { mutableStateOf(false) }
val suggestions = listOf("Item1","Item2","Item3")
var selectedText by remember { mutableStateOf("") }

var textfieldSize by remember { mutableStateOf(Size.Zero)}

val icon = if (expanded)
Icons.Filled.ArrowDropUp //it requires androidx.compose.material:material-icons-extended
else
Icons.Filled.ArrowDropDown


Column() {
OutlinedTextField(
value = selectedText,
onValueChange = { selectedText = it },
modifier = Modifier
.fillMaxWidth()
.onGloballyPositioned { coordinates ->
//This value is used to assign to the DropDown the same width
textfieldSize = coordinates.size.toSize()
},
label = {Text("Label")},
trailingIcon = {
Icon(icon,"contentDescription",
Modifier.clickable { expanded = !expanded })
}
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.width(with(LocalDensity.current){textfieldSize.width.toDp()})
) {
suggestions.forEach { label ->
DropdownMenuItem(onClick = {
selectedText = label
}) {
Text(text = label)
}
}
}
}
enter image description here
enter image description here

关于android - Jetpack compose 的公开下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67111020/

25 4 0