Custom spinner items not included with Spinner

I have a problem: when I use the non-trivial type of the Spinner element, Spinner displays a drop-down list somewhere other than 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 issue I report here is slightly different. I divided them so it is clear where to send different answers)

My goal was to have a slightly more bizarre display for each element in the spinner, and so I started by creating a layout that contains several elements, one of which is the target TextView (lbl2, in this case)

   

Then I try to configure Spinner (my final goal is to populate the spinner programmatically, so I don't use the resources to configure it) 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 - Spinner, in fact, displays a dull-looking multi-color vertical layout of three text views for each element and correctly replaces Item # 1, Item # 2 and No. 3 for lbl2. I can even click on the counter and open a drop-down list of options.

This problem is that items are not displayed on top of the counter. Instead, they just float over the activity, a little further. We hope that this picture will help clarify: Spinner Floating Elements http://www.freeimagehosting.net/uploads/bf9f584156.png

EDIT: - , !

+3
1

Android Spinners ... " ".

, XML- ( spinner):

android:prompt="Select a fancy item..."
+1

Source: https://habr.com/ru/post/1775036/


All Articles