繁体   English   中英

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 着色:

Step 1:创建一个drawable资源文件,以位图为父元素如下图,在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步:
在主题下拉菜单下选择自定义

第 3 步:
然后在前景色选择中选择颜色为#FFFFFF。方法 2 图形表示

最后完成向导添加图像,然后将 drawable 添加为图像。

回答问题的图示

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

所以我的解决方案是在构造函数中设置颜色过滤器,如下所示:

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

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

我能够使用MaterialButton (来自材料支持库,大多数人可能已经在使用)完成同样的事情。 MaterialButton有一个名为 icon 的属性,可以放置在左/中/右(默认为左)。 它还有一个名为 iconTint 的属性,它将为图标着色。

摇篮:

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"

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

我已经用一些草稿代码扩展了 AppCompatButton。 TintAppCompatButton应该具有默认行为,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;

希望这可以帮助。 这是我找到的最简单的方法。 还要注意,在您的应用程序中使用此图标的任何地方,都将是这种颜色。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM