Wrapping text in spinner android [duplicate]

2019-07-20 03:04发布

问题:

This question already has an answer here:

  • Spinner does not wrap text — is this an Android bug? 14 answers

I have long text that I want to put in a few, but not all, items in a spinner. Tried using the answer here but was not able to get it to work. I created and saved multiline_spinner_dropdown_item.xml in res/layout directory but still get an error "multiline_spinner_dropdown_item cannot be resolved" in Eclipse.

回答1:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1" 
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textColor="#ffffff"/>

Use this textview in your spinner adapter in place of the android default textview you are using in .setAdapter method of the Spinner. see below R.layout.spinner_textview textview is this textview i have posted above.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), 
                                                                         R.array.Array, 
                                                                        R.layout.spinner_textview);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);