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

时间:2017-10-12 15:56:17

标签: android

我想以编程方式从我的按钮更改我的图标颜色......

在我的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 个答案:

答案 0 :(得分:6)

首先,除非您编写自定义视图并且想要扩展它,否则不要直接使用AppCompatButton。正常Button将被解析&#34;系统为AppCompatButton,因此您不需要后者。

至于你原来的问题,有多种方法可以为一个drawable着色。您可以使用DrawableCompact在&#34;着色&#34;中进行此操作。时尚,而你可以使用普通的ColorFilter在&#34;过滤&#34;方式。

使用DrawableCompat

着色

DrawableCompat用于wrap drawable,以便在较旧的平台上tinted

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(...)方法为您的drawable设置重叠的滤色器。

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 :(得分:4)

我将矢量drawable用作Button的drawableLeft,并使用 Kotlin 这样通过编程方式更改了按钮drawable的颜色。

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

答案 2 :(得分:0)

我假设您需要更改android:drawableTint属性。

根据this,您需要创建一个具有不同色调的新drawable,然后更改按钮的可绘制资源。

从您的图标中创建Drawable

Drawable mDrawable=getContext().getResources().getDrawable(R.drawable.ic_car_black_24dp); 

然后改变它的色调:

mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

您已完成此操作,请设置新的Drawable

yourButton.setImageDrawable(mDrawable);

我建议您浏览关联问题和here in the docs的评论,以发现不同的PorterDuff模式。

答案 3 :(得分:0)

使用setCompoundDrawableTintList属性更改颜色 我用它如下

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

答案 4 :(得分:0)

您可能想根据您的问题,通过使用kotlin的数据绑定适配器检查我的方法来动态更改按钮颜色。签出here

我也提到并引用了原始答案,并提供了替代解决方案或对变通办法有更好的了解的文章

答案 5 :(得分:0)

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));
        }
    }
}