How can I manage the height of android spinner items?

空扰寡人 提交于 2019-12-05 13:27:04

问题


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.

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 to 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 it's own padding of the list item.

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?


回答1:


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);



回答2:


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.

Here's a snippet from 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.



来源:https://stackoverflow.com/questions/3033422/how-can-i-manage-the-height-of-android-spinner-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!