开发者

Custom spinner items not appearing as part of Spinner

开发者 https://www.devze.com 2023-01-25 19:41 出处:网络
I\'m running into a problem: when I use a non-trivial type of Spinner item, the Spinner displays the drop-down list someplace other than on the Spinner.

I'm running into a problem: when I use a non-trivial type of Spinner item, the Spinner displays the drop-down list someplace other than on the Spinner.

(Note: most of this description is identical to https://stackoverflow.com/questions/4188443/android-doesnt-honor-selection-change-with-custom-spinner-items, but the problem I'm reporting here is slightly different. I've split these up so it's clear where to direct different replies to)

My goal was to have a slightly more fancy display for each item in the spinner, and so I started by creating a layout that contains several i开发者_StackOverflow社区tems, one of which is the target TextView (lbl2, in this case)

I then attempt to set up the Spinner (my eventual goal is to populate the spinner programmatically, so I'm not using resources to set this up) using:

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
            this, R.layout.spinner_fancy, R.id.lbl2);
    adapter.add("Item #1");
    adapter.add("Item #2");
    adapter.add("Item #3");
    spinner.setAdapter(adapter);

When I run the program it looks (mostly) good - the Spinner is, in fact, rendering the goofy-looking multi-color, vertical layout of three textviews for each item, and it's correctly substituting Item #1, Item #2, and Item #3 for lbl2. I can even click on the spinner & bring up the drop-down list of choices.

This problem is that the items aren't displayed over the spinner. Instead they're just kind of floating over the activity, a bit further down. Hopefully this picture will help clarify: Floating Spinner Elements http://www.freeimagehosting.net/uploads/bf9f584156.png

EDIT: Thanks for the vote up - I've fixed up the image so it's now inline!


Android Spinners work in that way... The list of items is shown in "dialog mode".

You can add a title to the list using this in the XML layout (in the spinner section):

android:prompt="Select a fancy item..."
0

精彩评论

暂无评论...
验证码 换一张
取 消