android spinner dialog popup background

孤街醉人 提交于 2021-02-10 14:10:52

问题


My android app already set popupbackground as drawable xml. However, the popup dialog still cannot show the color I set. How to settle this issue?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CountrySelectorActivity">


    <Spinner
        android:id="@+id/search_spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/black"
        android:popupBackground="@drawable/spinner_background"
        android:spinnerMode="dialog"
        />

    <Spinner
        android:id="@+id/search_spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/black"
        android:popupBackground="@drawable/spinner_background"
        android:spinnerMode="dialog"/>

</LinearLayout>

@drawable/spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/green"/>

        </shape>
    </item>

</selector>

Spinner activity code https://stackoverflow.com/questions/51495271/android-kotlin-spinner-working-for-api-23-but-not-working-for-api-21


回答1:


Make a style using your custom spinner background drawable. Then add the style as an attribute to your spinner. Lastly, programmatically change the spinner popup background color in your activity or fragment. The following way worked for me:

<style name="SpinnerTheme" parent="android:Widget.DeviceDefault.Spinner">
    <item name="android:background">@drawable/spinner_background</item>
    <item name="android:padding">8dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:paddingRight">15dp</item>
</style>

Put this in your xml for each spinner (REMOVE android:popupBackground="@drawable/spinner_background" & android:spinnerMode="dialog"):

<Spinner
    android:id="@+id/search_spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:background="@color/black"
    style="@style/SpinnerTheme"/>

Then in your activity or fragment, programmatically set the popup background color:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                spinner.setPopupBackgroundResource(R.color.yourColor);
            }

Here is a link to example: https://www.oodlestechnologies.com/blogs/Custom-Spinner-In-Android




回答2:


With AndroidX and using AppCompatSpinner you can do this:

File: styles.xml

<style name="MyPopUpTheme">
    <item name="android:background">@color/background_spinner</item>
    <item name="android:padding">5dp</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:textStyle">bold</item>

</style>

File: Your fragment or Activity

<androidx.appcompat.widget.AppCompatSpinner
    android:id="@+id/spinner_years"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="50dp"
    android:background="@drawable/spinner_bg"
    android:padding="15dp"
    android:theme="@style/MyPopUpTheme" />

Result:



来源:https://stackoverflow.com/questions/51499001/android-spinner-dialog-popup-background

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