如何在Button上更改CompoundDrawable的颜色? [英] How to change color of CompoundDrawable on Button?

查看:138
本文介绍了如何在Button上更改CompoundDrawable的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何更改按钮左侧可绘制图标的颜色。

I am trying to figure out how to change the color of icon which is in drawable left of button.

以下是我正在使用的XML代码:

Below is the XML code I am using :

  <Button
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_toRightOf="@+id/student_images"
        android:drawableLeft="@mipmap/ic_email_black_18dp"
        android:text="   myemail@gmail.com  "
        android:layout_below="@+id/email"
        android:background="#00000000"
        android:layout_marginBottom="20dp"
        android:fontFamily="sans-serif"
        android:textColor="@color/gray_text_color"
        />

我尝试了 android:tint 但图标的颜色没有改变。我被困在这里。

I have tried the android:tint but color of icon is not changing. I am stuck here.

推荐答案

您可以像下面这样以编程方式设置色调:

You can set tint programmatically like below:

int tintColor = ContextCompat.getColor(context, android.R.color.darker_gray);

Button button = (Button) findViewById(R.id.button);

Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.ic_email_black_18dp);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), tintColor);

drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

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

或者您可以使用库支持可绘制的着色笔通过snodnipper。

此库可为button的drawableLeft设置色调。

https://github.com/snodnipper/android-appcompat-issue198613

Or you can use library Support Drawable Tints by snodnipper.
This library enables to set tint for drawableLeft of Button.
https://github.com/snodnipper/android-appcompat-issue198613

这篇关于如何在Button上更改CompoundDrawable的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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