【发布时间】:2021-04-06 09:17:57
【问题描述】:
我目前正在使用dropdownmenu 在我的应用中选择众多选项之一。问题在于,一旦其中包含许多元素,下拉菜单就会在最初锚定的位置上方扩展(请参见附图)。如何强制下拉菜单像第一张图片一样固定在按钮的底部?谢谢!
what i want (only works with few items in the list)
the problem (occurs when there is lots of items in the list)
编辑:这里是相关代码:
Box(modifier = Modifier.fillMaxWidth()) { // box for dropdown menu
Button(
modifier = Modifier.fillMaxWidth(),
onClick = { showCategories = true }
) {
Icon(Icons.Default.Favorite, null)
Text("${currentCategory.name}")
Icon(
painter = painterResource(id = R.drawable.ic_baseline_expand_more_24),
contentDescription = "expand more"
)
}
DropdownMenu(
expanded = showCategories,
onDismissRequest = {
showCategories = false
}
) {
categories.forEach {
DropdownMenuItem(
onClick = {
onCategoryChangeRequest(it)
showCategories = false
}
) {
Text(it.name)
}
}
}
}
【问题讨论】:
-
请提供一些您目前所做的代码。
-
@Y.Kakdas 我刚刚添加了相关代码。我的实现基于文档中的示例。他们声明要使用一个框,然后您的下拉菜单应锚定到框中的另一个可组合项(在我的情况下为 Button)。在对 DropDownMenu 实现进行了更多阅读之后,我认为如果它没有足够的空间,它会扩展到屏幕底部,如果仍然不够的话,它会扩展到屏幕顶部。我只是希望它无论如何都保持锚定,因为它毕竟是一个可滚动列表,并且应该保持锚定到它所属的按钮。
-
我认为我们无法改变它。在
DropDownMenu内部定义了一个DropdownMenuPositionProvider。 -
@GabrieleMariotti 是的,我最终复制了整个 DropDownMenu 实现,然后尝试编写自己的 DropdownMenuPositionProvider,但我无法获得我想要的行为。不过谢谢!
标签: android kotlin android-jetpack android-jetpack-compose