【问题标题】:Change color of the drop down arrow of Spinner in XML在 XML 中更改 Spinner 下拉箭头的颜色
【发布时间】:2015-11-16 22:26:18
【问题描述】:

正如我在问题中所写,我想更改Spinnerin XML 的下拉箭头(默认箭头,而不是自定义箭头或类似的东西)的颜色,但问题是我无法从XML 中找到任何可以引用它的内容。

有可能吗?如果是,如何更改颜色?

提前致谢。

【问题讨论】:

  • 看看答案here
  • @Rayes 但它是用于自定义下拉菜单,而不是默认下拉菜单。
  • @Error404 3 个月后,我面临同样的问题。您接受的答案帮助我解决了问题。 1 票给你 1 票给侯赛因 :)

标签: android android-layout colors android-spinner android-color


【解决方案1】:

有三种方法可以实现。

1.通过代码:

在您的 xml 中,确保您的微调器有一个 id。假设我们有一个 id 为“spinner”的微调器。

在您的代码中,在 onCreate() 中添加以下内容:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

其中红色是您在 values 文件夹的 colors.xml 中定义的颜色。

2。通过xml:

对于 API 21+:

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/red" />

或者如果你使用支持库,你可以使用:

<android.support.v7.widget.AppCompatSpinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/red" />

3.通过drawables:

您可以使用这个在线工具:http://android-holo-colors.com

这将为您希望使用首选颜色的任何视图生成自定义可绘制对象。确保选择微调器,然后下载资源。

【讨论】:

  • 我没有colors.xml 文件。还是我必须创建它?另外我想在我的问题中指出,当我说 by XML 时,我的意思是是否有像 android:colorDropDownArrow 这样的选项或类似的东西来直接在 XML 中设置箭头的值。谢谢!
  • 不幸的是,除了使用 android:background="@drawable/spinnerBackground" 之外,您无法从您的 xml 中执行此操作,但这会完全改变您的微调器背景.因此,您必须在名为 colors.xml 的值文件夹中创建一个新的 xml,并在其中添加一种称为红色的颜色。另请检查我更新的答案,如果您愿意,我已经提出了另一种方法,而不是创建 colors.xml
  • 感谢您的更新!但我仍有疑问。如果我没有 Spinner 的任何背景,它只会与颜色一起显示箭头,但如果我制作背景,箭头就会消失。你知道为什么吗?
  • 如果您为微调器制作背景,这意味着您将删除其默认背景,即箭头。因此,如果您想在 xml 中使用背景属性,那么您应该采用我在答案中发布的第二种方式。该站点将生成一个 zip 文件,其中包含您将放入项目中的文件。该网站将向您解释。如果您需要更多帮助,请告诉我。
  • 我将其标记为已接受,因为您非常有帮助,如果我有疑问,我会发表新评论;)
【解决方案2】:

我很惊讶没有人指出这一点,但你可以继承 Widget.AppCompat.Spinner 并更改 backgroundTint

<style name="Spinner" parent="Widget.AppCompat.Spinner">
        <item name="backgroundTint">@color/spinnerTint</item>
</style>

并将其应用于Spinner

<Spinner
    style="@style/Spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown" />

【讨论】:

  • backgroundTint 属性不是仅适用于 API 级别 21 吗?
  • @niranjan_b android:backgroundTint 是。这是 AppCompat 版本(注意这里没有 android 前缀,样式扩展了 AppCompat 派生样式)
  • 奇怪的行为:当我添加属性 android:background 时,无论我设置的背景是什么值,背景的颜色都是从 backgroundTint 中获取的,因此看不到箭头。
  • @Zvi 当您更改 android:background 时,您不再使用 Android 的背景可绘制对象,而是定义了一个新的颜色可绘制对象,这就是您看不到箭头的原因
  • 是的,我在阅读其他帖子时意识到这一点。我还发现一个帖子解决了我的问题,如下所示:为了在不破坏可绘制对象的情况下更改背景颜色,我将微调器放在相对布局中并定义了它的背景颜色。这与您使用 backgroundTint 更改箭头颜色的解决方案相结合,可以完全控制两种颜色。
【解决方案3】:

试试这个:

spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this,
                R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);

【讨论】:

  • 它改变了背景颜色而不是箭头的颜色
【解决方案4】:
<Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#00000" />

仅适用于 API 级别 21 以上

【讨论】:

    【解决方案5】:

    使用 backgroundTint 属性

    <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            />
    

    如果 min_SDK AppCompatSpinner

    <android.support.v7.widget.AppCompatSpinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:backgroundTint="@color/white"
            /> 
    

    【讨论】:

      【解决方案6】:

      如果API 21+){

      您可以在 Spinner 中直接使用 android:backgroundTint="@color/color"

      <Spinner
         android:id="@+id/spinner"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:backgroundTint="@color/color" />
      

      } 其他 {

      创建自己的风格:

      <style name="spinner_style" parent="Widget.AppCompat.Spinner">
              <item name="backgroundTint">@color/color</item>
      </style>
      

      然后进入 Spinner:

      <Spinner
         android:id="@+id/spinner"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         style="@style/spinner_style"/> 
      

      }

      注意:您可以在所有 API 中使用 style 方法。

      【讨论】:

        【解决方案7】:

        使用this 依赖创建一个非常漂亮和简单的微调器和 使用 "app:arrowTint="@color/green" 改变箭头颜色

        【讨论】:

        • 试过用了,根本不显示列表。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-11
        • 2020-01-12
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        相关资源
        最近更新 更多