在XML中更改Spinner下拉箭头的颜色 [英] Change color of the drop down arrow of Spinner in XML

查看:469
本文介绍了在XML中更改Spinner下拉箭头的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在问题中所写的那样,我想更改XML中的Spinner 的下拉箭头(默认箭头,而不是自定义箭头或类似的东西)的颜色,但是问题是我无法从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);

其中红色是您在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.通过可绘制对象:

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

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

As I wrote in my question, I want to change the color of the drop down arrow (the default arrow, not a custom arrow or something like that) of a Spinner in XML, but the problem is that I couldn't find anything to make reference to it from the XML.

Is it possible? If yes, how can I change the color?

Thanks in advance.

解决方案

There are three ways to achieve that.

1. Through code:

In your xml, make sure your spinner has an id. Let's say we have a spinner with id "spinner".

In your code, add the following in your onCreate():

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

where red is your defined color in colors.xml in the values folder.

2. Through xml:

For API 21+:

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

or if you use the support library, you can use:

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

3. Through drawables:

You can use this online tool: http://android-holo-colors.com

This will generate custom drawables for any view you want with your preferred color. Make sure you select spinner, then download the resources.

这篇关于在XML中更改Spinner下拉箭头的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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