更改xml drawable图标的颜色

Ant*_*ema 7 android android-xml

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

 <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
  />
Run Code Online (Sandbox Code Playgroud)

isc*_*iot 9

您可以使用此代码段

<?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>
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不适用于矢量drawables (2认同)

hrs*_*krs 5

android L (5.0)有一个TINT功能,它允许更改的颜色drawable icon.你可以在这里查看一个例子.

对于早期APIs,你必须使用multiple drawablesselector策略


Fel*_*res 5

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

<ImageView  android:layout_width="32dp"
            android:layout_height="32dp"
            android:src="@drawable/ic_drawable_resource_name"
            android:tint="@color/color_name"/>
Run Code Online (Sandbox Code Playgroud)