I solved this issue by the following method.
- API level 22 or lower: Use
app:popupTheme
- API level 23 or higher: Use
android:popupTheme
res/layout/spinner.xml
<android.support.v7.widget.AppCompatSpinner
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="@dimen/toolbar_spinner_margin_right"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
res/layout-v23/spinner.xml
<android.support.v7.widget.AppCompatSpinner
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="@dimen/toolbar_spinner_margin_right"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
res/layout/activity_main.xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="@layout/spinner" />
</android.support.v7.widget.Toolbar>
And to solve an issue where the spinner button color is black in API level 19 or lower, you need to add the following code:
Kotlin
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
val spinnerDrawable = spinner.background.constantState.newDrawable()
spinnerDrawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spinner.background = spinnerDrawable
} else {
spinner.setBackgroundDrawable(spinnerDrawable)
}
}
Java
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();
spinnerDrawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spinner.setBackground(spinnerDrawable);
} else {
spinner.setBackgroundDrawable(spinnerDrawable);
}
}