简体   繁体   中英

Change popupbackground color of dialog spinner

I want to change popupBackground color of my dialog spinner.

In my activity.xml :

<Spinner
                    android:id="@+id/mCategorySpinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/textView7"
                    android:entries="@array/recipeCategory"
                    android:spinnerMode="dialog"
                    android:popupBackground="@color/colorPrimary"
                    android:textAlignment="center" />

In my activity.java :

categorySpinner=(Spinner) findViewById(R.id.mCategorySpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.recipeCategory, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categorySpinner.setPrompt("Choose category");
        categorySpinner.setAdapter(new NothingSelectedSpinnerAdapter(
                adapter,
                R.layout.category_spinner_row_nothing_selected,             
                this));

Nothing happens if I change android:popupBackground in XML,it remains the default white.
But if I change background it works, but it's not for dialog's background.

You can change background color and drop down icon like doing this way

Step 1: In drawable folder make a file called background.xml for the background of the spinner.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <corners android:radius="5dp" />
    <stroke
        android:width="1dp"
        android:color="@color/darkGray" />
</shape>

Step 2: Now apply this background on your spinner in xml file

 android:background="@drawable/background"

1.Use spinner_selector.xml

To show the color your changed

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_red_light"
          android:state_pressed="true"/>
    <item android:color="@android:color/white"
          android:state_pressed="false"/>
</selector>

2.Add style

Add it to the style ,and you can use it in other place .

<style name="spinner_style">
   <item name="android:background">@drawable/spinner_selector</item>
</style>

3.Add it to the xml code

Use it as background of spinner .

<Spinner
    android:id="@+id/mCategorySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView7"
    android:entries="@array/recipeCategory"
    android:spinnerMode="dialog"
    style="@style/spinner_style"
    android:textAlignment="center" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM