【问题标题】:Change popupbackground color of dialog spinner更改对话框微调器的弹出背景颜色
【发布时间】:2019-06-20 20:42:57
【问题描述】:

我想更改对话框微调器的 popupBackground 颜色。

在我的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" />

在我的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));

如果我在 XML 中更改 android:popupBackground,则不会发生任何事情,它仍然是默认的白色。
但是,如果我更改背景,它会起作用,但它不适用于对话框的背景。

【问题讨论】:

标签: java android xml spinner


【解决方案1】:

你可以像这样改变背景颜色和下拉图标

第 1 步:在 drawable 文件夹中创建一个名为 background.xml 的文件作为微调器的背景。

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

第 2 步:现在将此背景应用到 xml 文件中的微调器上

 android:background="@drawable/background"

【讨论】:

  • drawable文件夹中的XML资源像图片一样被访问,不需要在名字后面加上.xml。这实际上导致它找不到。
【解决方案2】:

1.使用 spinner_selector.xml

显示你改变的颜色

<?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.添加样式

将其添加到样式中,您可以在其他地方使用它。

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

3.添加到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"
    style="@style/spinner_style"
    android:textAlignment="center" />

【讨论】:

  • 没有描述。现在有一些描述,我删除了反对票。请记住,并非所有将来阅读本文的人都确切地知道每件事的作用,解释它很重要。它仍然需要一个更好的解释,但现在它已经足够好了,不会被否决
  • @drawable/spinner_press@drawable/spinner 怎么样?
  • 我把它改成颜色。如果你按下,颜色会改变。@Chris
  • 你能检查我的答案吗?
猜你喜欢
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多