How can I change the background color of a spinner popup?

隐身守侯 提交于 2019-12-01 15:23:55

Use android:popupBackground="@drawable/Yourxmlfile.xml" in Your spinner declaration of xml .

For Eg.

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:background="@drawable/gradient_spinner"
        android:gravity="center"
        android:paddingLeft="40dp"
        android:popupBackground="@drawable/radialfront"
        >

None of the above solutions worked for me.

The solution was writing in the adapter passed to the spinner a different implementation in the method getDropDownView to display a different layout with custom colors:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vista = convertView;       

    // layout for spinner widget

    if (vista==null) {
        LayoutInflater inflater = actividad.getLayoutInflater();
        vista = inflater.inflate(R.layout.fila_colores_spinner, null);                  
    }

    return vista;
}

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
    View vista = convertView;       

    //layout for spinner popup

    if (vista==null) {
        LayoutInflater inflater = actividad.getLayoutInflater();
        vista = inflater.inflate(R.layout.fila_colores_spinner_popup, null);                    
    }

    return vista;
}

It works great!!

  • declare layout with text view with the wanted color (and other attributes you want to change)
  • the id of the textView must be text1 (cause this is the id that is declare in the default layout in R.android for your spinner)
  • give this layout in your spinner adapter

for example:

  • in res\layout\custom_spinner_item.xml put this:

    TextView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@android:id/text1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#B2B5B7"
                    android:gravity="center_horizontal"
                     android:textSize="20dp"
    

then give this layout to the adapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
      MyActivity.this,
      R.layout.custom_spinner_item, strings);
  • then give this adapter to your spinner
  <Spinner
      android:id="@+id/myspinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/list_selector"      
      android:drawSelectorOnTop="true" />

try this..

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