Android Spinner对话框弹出背景 [英] android spinner dialog popup background

查看:79
本文介绍了Android Spinner对话框弹出背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序已将popupbackground设置为可绘制的xml.但是,弹出对话框仍然无法显示我设置的颜色.如何解决这个问题?

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

@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>

纺锤活动代码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>

为此在每个转盘中添加XML(删除android:popupBackground ="@ drawable/spinner_background"&android:spinnerMode ="dialog"):

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);
            }

这是示例的链接: https://www.oodlestechnologies.com/blogs/Android中的Custom-Spinner-in

这篇关于Android Spinner对话框弹出背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆