以编程方式更改Android按钮可绘制图标的颜色

18

我希望能够通过编程来改变我的按钮图标颜色...

在我的xml中,我有:

            android:drawableTint="@color/colorPrimary"
            android:drawableTop="@drawable/ic_car_black_24dp"

如何设置图标并更改图标颜色...但我想从Java端更改图标颜色...

有人可以帮帮我吗?

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/bt_search_vehicle_car"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/eight_density_pixel"
            android:layout_weight="1"
            android:background="@drawable/background_rounded_blue_border"
            android:drawableTint="@color/colorPrimary"
            android:drawableTop="@drawable/ic_car_black_24dp"
            android:padding="@dimen/eight_density_pixel"
            android:text="Carros"
            android:textAllCaps="false"
            android:textColor="@color/colorPrimary" />
6个回答

20
首先,除非您编写自定义视图并希望扩展它,否则不要直接使用AppCompatButton。正常的Button将被系统解析为AppCompatButton,因此您不需要后者。
至于您最初的问题,有多种方法可以对可绘制对象进行着色。您可以使用DrawableCompact以一种“着色”方式进行处理,而您可以使用普通的ColorFilter来以“过滤”方式进行处理。
使用DrawableCompat进行着色
使用DrawableCompat包装可绘制对象,以便在旧平台上对其进行着色
Button yourButton = findViewById(R.id.bt_search_vehicle_car);

Drawable drawable = getResources().getDrawable(R.drawable.ic_car_black_24dp);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, getResources().getColor(R.color.colorPrimary));

yourButton.setCompoundDrawables(null, drawable, null, null);

使用ColorFilter

使用Drawable.setColorFilter(...)方法为您的可绘制对象设置覆盖色过滤器。

Button yourButton = findViewById(R.id.bt_search_vehicle_car);

Drawable drawable = getResources().getDrawable(R.drawable.ic_car_black_24dp).mutate();
drawable.setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);

yourButton.setCompoundDrawables(null, drawable, null, null);

1
嗯,setCompoundDrawables 只有在我们配置矩形边界时才起作用。解决方法是使用 setCompoundDrawablesWithIntrinsicBound,如果我们不想明确指定边界。 - mochadwi

12

使用 setCompoundDrawableTintList 属性来改变颜色,我是这样使用的:

btn_recent.setCompoundDrawableTintList(ColorStateList.valueOf(Color.parseColor("#ff9708"))); 

1
setCompoundDrawableTintList需要"min Api = 23"。 - Nabzi

5

我使用矢量图作为按钮的drawableLeft,并使用Kotlin编程来动态更改按钮绘制颜色,代码如下。

button_id.compoundDrawableTintList = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.blue))

需要 Android M(23)以上 - mr.kostua

3
public void setTextViewDrawableTintColor(TextView textView, int color) {
    for (Drawable drawable : textView.getCompoundDrawables()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
        }
    }
}

运行得非常好! - hetsgandhi

1
我假设你需要更改android:drawableTint属性。
根据this,您需要创建一个带有不同色调的新可绘制对象,然后更改按钮的可绘制资源。
从您的图标创建一个Drawable
Drawable mDrawable=getContext().getResources().getDrawable(R.drawable.ic_car_black_24dp); 

然后改变它的色调:
mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

一旦完成这个步骤,设置你的新Drawable:
yourButton.setImageDrawable(mDrawable);

我建议您浏览链接问题的评论以及这里的文档,以了解不同的PorterDuff模式。


我需要设置另一张图片吗?我不能只更改图像的颜色吗? - Felipe A.
你需要一个不同的可绘制对象来完成这个任务。我猜你需要改变 ic_car_black_24dp 图标,对吗? - magicleon94
不,我只需要更改ic_car_black_24dp的颜色...当我使用drawableTint时,我可以改变颜色...但在xml中...我没有找到任何属性来在Java端更改它 - Felipe A.
好的,我的错。让我检查一些东西,然后我会编辑我的答案。 - magicleon94
正在处理中...等我完成实现后,我会给你反馈,请稍等几分钟。 - Felipe A.
我发现了setCompoundDrawablesWithIntrinsicBounds可以更改我的可绘制图像...现在我只需要更改图像...但是这个方法不起作用。 - Felipe A.

0

根据你的问题,你可能想要查看我的动态改变按钮颜色的方法,它是基于Kotlin的数据绑定适配器实现的。在这里查看

我还提到并引用了原始答案以及另一种解决方案的文章,或者可以更好地理解这个解决方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接