Android 按钮可绘制色调

Android Button Drawable Tint

是否可以为 android 按钮中的 drawableLeft 着色?我有一个黑色的可绘制对象,我想染成白色。我知道如何使用图像视图(左侧图像)实现此目的,但我想使用默认的 android 按钮来实现。

我的源代码:

<Button android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="@string/drawer_quizzes"
        android:backgroundTint="@color/md_light_green_500"
        android:stateListAnimator="@null"
        android:textColor="#fff"
        android:textSize="12dp"
        android:fontFamily="sans-serif"
        android:drawableLeft="@drawable/ic_action_landscape"
        android:gravity="left|center_vertical"
        android:drawablePadding="8dp"
        />

有没有办法给按钮着色?或者有没有自定义视图的方法可以实现这种效果?

您可以使用以下方法实现为按钮上的 drawableleft 着色:

第一步: 创建一个以位图为父元素的可绘制资源文件,如下所示并命名 作为drawable文件夹下的ic_action_landscape.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/ic_btn_speak_now"
    android:tint="@color/white" />

第 2 步: 在您的布局中创建您的 Button 控件,如下所示

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/md_light_green_500"
        android:drawableLeft="@drawable/ic_action_landscape"
        android:drawablePadding="8dp"
        android:fontFamily="sans-serif"
        android:gravity="left|center_vertical"
        android:stateListAnimator="@null"
        android:text="@string/drawer_quizzes"
        android:textColor="#fff"
        android:textSize="12dp" />

按钮从 drawable 文件夹中的 ic_action_landscape.xml 获取 drawable 而不是 @android:drawable 或 drawable png(s)。

方法二:
第 1 步:
您甚至可以将图标添加为操作栏和带有前景作为图像的选项卡图标 可以从自定义位置或剪贴画导入

第 2 步:
在主题下拉菜单下 select 自定义

第 3 步:
然后select前景色selection中的颜色为#FFFFFF。

最后完成添加图像的向导,然后将可绘制对象添加为图像。

正如@Boris 所建议的,您可以使用支持库。

我扩展了 AppCompatButton,其中一些 draft code. The TintAppCompatButton should have default behaviour but TintAppCompatButtonCustom 用于演示自定义着色。

我会看看是否可以提出 PR 以将其纳入官方支持库。

感兴趣的代码是:

private void init() {
    PorterDuff.Mode tintMode = PorterDuff.Mode.SRC_IN;
    Drawable[] ds = getCompoundDrawables();
    tint(ds[LEFT], Color.RED, tintMode);
    tint(ds[TOP], Color.YELLOW, tintMode);
    tint(ds[RIGHT], Color.GREEN, tintMode);
    tint(ds[BOTTOM], Color.BLUE, tintMode);
}

private void tint(Drawable d, int color, PorterDuff.Mode tintMode) {
    TintInfo ti = new TintInfo();
    ti.mTintMode = tintMode;
    ti.mTintList = ColorStateList.valueOf(color);
    ti.mHasTintList = true;
    ti.mHasTintMode = true;

    TintManager.tintDrawable(d, ti, new int[]{0});
}

我可能会迟到,但如果你使用 C#,你可以这样做:

Color iconColor = Color.Orange; // TODO: Get from settings
Mode mode = Mode.SrcAtop;

Drawable drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.ic_action_arrest, null);

drawable.SetColorFilter(iconColor, mode);

您还需要:

using Android.Graphics;
using static Android.Graphics.PorterDuff;
using Android.Graphics.Drawables;

希望这对您有所帮助。这是我找到的最简单的方法。 另请注意,您在应用中使用此图标的任何地方都将是此颜色。

我知道已经有很多答案,但没有一个让我高兴,因为我不想为不同样式的元素使用不同的可绘制对象。

所以我的解决方案是像这样在构造函数中设置滤色器:

int textColor = getTextColors().getColorForState(EMPTY_STATE_SET, Color.WHITE);
Drawable[] drawables = getCompoundDrawablesRelative();
for (Drawable drawable : drawables) {
    if (drawable != null) {
        drawable.setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
    }
}

我使用了文本颜色,因为这是我需要的,但可以用自定义属性替换它以使其更加动态。

您可以在 >23 android 版本中使用 android:drawableTint="@color/yourColor"

我能够使用 MaterialButton (from the material support library, which most people are probably using already). MaterialButton 完成同样的事情,它有一个名为 icon 的属性,可以放置在 left/center/right(默认左侧)。它还具有一个名为 iconTint 的属性,该属性将为图标着色。

Gradle:

implementation "com.google.android.material:material:1.1.0-alpha09"

查看:

<com.google.android.material.button.MaterialButton
    android:layout_width="0dp"
    android:layout_weight="1"
    android:text="@string/drawer_quizzes"
    android:backgroundTint="@color/md_light_green_500"
    android:stateListAnimator="@null"
    android:textColor="#fff"
    android:textSize="12dp"
    android:fontFamily="sans-serif"
    app:icon="@drawable/ic_action_landscape"
    app:iconTint="@color/white"
    android:gravity="left|center_vertical"
    android:drawablePadding="8dp"
    />

您可以使用可绘制过滤器,或者如果您的 API>=M 那么您可以简单地

textView.compoundDrawableTintList = ColorStateList.valueOf(Color.WHITE)

或在XML、

android:drawableTint="@color/white"