Spinner with long text not working fine

我的梦境 提交于 2019-12-07 04:50:09

问题


I have some problems with the spinner. Depending of my dates, I must add to a TableRow a TextView with an EditText or a Spinner. My array that must be display in Spinner is a little long. I tested my code with an array with short texts, and it looks like this :

Here the single problem is that spinner is not fill_parent.

If I put my array to spinner it looks like this :

In this case, the spinner doesn't look like a spinner and the EditText is not visible any more. When I choose the spinner, it appears this view :

Here I need to display all the text of the array. This is my code :

TableRow.LayoutParams lp = new TableRow.LayoutParams(
            TableRow.LayoutParams.FILL_PARENT , TableRow.LayoutParams.WRAP_CONTENT);
tablerow_product[i] = new TableRow(viewToLoad.getContext());
tablerow_product[i].setLayoutParams(lp);

product_spinner[i] = new Spinner(viewToLoad.getContext());
product_spinner[i].setLayoutParams(lp);   product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
String[] proba={"red","blue"};  //first image is with this test array
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(),  com.Orange.R.layout.my_spinner_textview,spinnerArray);                                     spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
product_spinner[i].setAdapter(spinnerArrayAdapter);
tablerow_product[i].addView(product_spinner[i]);                                            Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,                  TableRow.LayoutParams.WRAP_CONTENT));

and my_spinner_textview.xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@drawable/textorange_selected"
    android:gravity="left"
    android:singleLine="false"
    android:ellipsize="end"/>

Can anyone help me to solve it? Any idea is welcome. Thanks in advance.


回答1:


For my problem I found this solution :

Spinner language = (Spinner) findViewById(com.Orange.R.id.current_language_text);

ArrayAdapter adapter = new ArrayAdapter(this,
                com.Orange.R.layout.my_spinner_textview, languages);
adapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);
language.setAdapter(adapter);

where languages is a String[] and my_spinner_textview.xml is :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview_spinner"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@drawable/textorange_selected" 
    android:paddingLeft="5dp"
    android:singleLine="true"
    android:ellipsize="end"
 />



回答2:


simply I did with custom text view like:

ArrayAdapter myAdapter = new ArrayAdapter(context, R.layout.text_view, reasonTypesList);
myAdapter.setDropDownViewResource(R.layout.text_view);
mySpinner.setAdapter(myAdapter);

and here is the text_view layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTextView"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding"
    android:text="Custom Text with multi lines" />


来源:https://stackoverflow.com/questions/11720665/spinner-with-long-text-not-working-fine

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