更改xml drawable图标的颜色

时间:2022-11-21 12:10:54

Is there any way of setting a color to an icon drawable? So that the icon's color would be overwritten by my custom color ?

有没有办法将颜色设置为可绘制的图标?那么图标的颜色会被我的自定义颜色覆盖?

 <item android:drawable="@drawable/icon1"
  //set color to my icon here
    android:state_pressed="true" />
<item android:drawable="@drawable/icon2"
  //set color to my icon here
  />

4 个解决方案

#1


You can use this snippet

您可以使用此代码段

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
    <item android:state_checked="true">
        <bitmap android:src="@drawable/signup_checkbox_full" android:tint="@color/turquoise" />
    </item>
    <item>
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
</selector>

#2


In android L (5.0) there is a TINT feature which allows to change the color of a drawable icon. You can check an example here.

在Android L(5.0)中有一个TINT功能,允许更改可绘制图标的颜色。你可以在这里查看一个例子。

For earlier APIs you have to use multiple drawables with selector strategy

对于早期的API,您必须使用具有选择器策略的多个drawable

#3


Try the following:

请尝试以下方法:

android:drawableTint="@color/icon_color"

#4


Use the tint attribute on your image element (or whatever you use to show an image), for example:

在图像元素上使用tint属性(或用于显示图像的任何内容),例如:

<ImageView  android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_drawable_resource_name"
            android:tint="@color/color_name"/>

#1


You can use this snippet

您可以使用此代码段

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
    <item android:state_checked="true">
        <bitmap android:src="@drawable/signup_checkbox_full" android:tint="@color/turquoise" />
    </item>
    <item>
        <bitmap android:src="@drawable/signup_checkbox_empty" android:tint="@color/turquoise" />
    </item>
</selector>

#2


In android L (5.0) there is a TINT feature which allows to change the color of a drawable icon. You can check an example here.

在Android L(5.0)中有一个TINT功能,允许更改可绘制图标的颜色。你可以在这里查看一个例子。

For earlier APIs you have to use multiple drawables with selector strategy

对于早期的API,您必须使用具有选择器策略的多个drawable

#3


Try the following:

请尝试以下方法:

android:drawableTint="@color/icon_color"

#4


Use the tint attribute on your image element (or whatever you use to show an image), for example:

在图像元素上使用tint属性(或用于显示图像的任何内容),例如:

<ImageView  android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_drawable_resource_name"
            android:tint="@color/color_name"/>