如何管理android微调项的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3033422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manage the height of android spinner items?
提问by rushinge
I have an android spinner that's populated by a list of strings using an ArrayAdapter and it operates fine, however because of the way the spinner is displayed I'm running into a display height problem with the list items.
我有一个 android 微调器,它由使用 ArrayAdapter 的字符串列表填充,并且运行良好,但是由于微调器的显示方式,我遇到了列表项的显示高度问题。
At first glance, it would seem that the ArrayAdapter can use a single layout for displaying options which leads to the problem I'm having. When displaying the current item in the spinner (when the user is not selecting a new item from the list) the spinner pads the text so that the spinner is a reasonable size for clicking on. However, when the user taps on it and brings up the list to select a new item, the list items presented are way too small height-wise. If I use an item layout that presents the list items at a reasonable height, then the spinner itself becomes exorbitantly huge due to its own padding of the list item.
乍一看,ArrayAdapter 似乎可以使用单一布局来显示选项,这会导致我遇到的问题。当在微调器中显示当前项目时(当用户没有从列表中选择新项目时),微调器会填充文本,以便微调器具有合理的点击大小。但是,当用户点击它并调出列表以选择新项目时,显示的列表项目在高度上太小了。如果我使用以合理高度呈现列表项的项目布局,则微调器本身由于其自身的列表项填充而变得非常巨大。
Any ideas on how I can manage the height of these two item display modes so that effectively they display with the same height value instead of the spinner height being larger than the list item display height?
关于如何管理这两种项目显示模式的高度以便它们有效地以相同的高度值显示而不是微调器高度大于列表项显示高度的任何想法?
回答by Pentium10
I've run into this issue myself a while ago, and it turned out that I need to use different layouts for dropdown and display
前段时间我自己也遇到过这个问题,结果我需要使用不同的布局进行下拉和显示
I have this code:
我有这个代码:
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cGroups,
new String[] {
"name", "_id"
}, new int[] {
android.R.id.text1
});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
回答by Tommi Kyntola
Yes, the above answer is correct.
是的,上面的答案是正确的。
It took me forever to find this, because it's wrong in the sdk samples for 2.2 Android. And I had a hard time accepting that.
我花了很长时间才找到这个,因为它在 2.2 Android 的 sdk 示例中是错误的。我很难接受这一点。
Here's a snippet from samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java:
这是来自 samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java 的片段:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets, android.R.layout.simple_spinner_dropdown_item);
while it should have android.R.layout.simple_spinner_item
there instead and simple_spinner_dropdown_item
should only be used for the dropdown items. Otherwise the spinner arrow get streched and it draws dropdown selection circle to the display, too.
而它应该在android.R.layout.simple_spinner_item
那里,并且simple_spinner_dropdown_item
应该只用于下拉项目。否则,微调箭头会被拉伸,它也会将下拉选择圆绘制到显示器上。