简体   繁体   English

Android Spinner对话框弹出背景

[英]android spinner dialog popup background

My android app already set popupbackground as drawable xml. 我的Android应用程序已将popupbackground设置为可绘制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 @ 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 微调器活动代码https://stackoverflow.com/questions/51495271/android-kotlin-spinner-working-for-api-23-but-not-working-for-api-21

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"): 将其放在每个微调器的xml中(删除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 这是示例的链接: https : //www.oodlestechnologies.com/blogs/Custom-Spinner-In-Android

With AndroidX and using AppCompatSpinner you can do this: 使用AndroidX并使用AppCompatSpinner,您可以执行以下操作:

File: styles.xml 文件: 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: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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