在 XML 中更改 Spinner 下拉箭头的颜色

Change color of the drop down arrow of Spinner in XML

正如我在问题中所写,我想更改 Spinner 中的下拉箭头(默认箭头,不是自定义箭头或类似箭头)的颜色XML,但问题是我无法从 XML.

中找到任何可以引用它的内容

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

提前致谢。

可以通过三种方式实现。

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

其中红色是您在值文件夹的 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。通过绘图:

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

这将为您想要的具有首选颜色的任何视图生成自定义可绘制对象。确保你 select 微调器,然后下载资源。

我很惊讶没有人指出它,但你可以继承 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" />

试试这个:

spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this,
                R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
<Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#00000" />

仅在 API 21 级以上有效

使用 backgroundTint 属性

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

如果 min_SDK < 21(Lollipop) 你应该使用 A​​ppCompatSpinner

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

If (API 21+) {

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

<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 中使用样式方法。

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