EditText Drawable Tint不可行?

14

我在整个应用程序中使用色彩渲染图标。

例如,在我的 ImageView 中:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/ic_account_circle"
    android:tint="@color/red_color"/>

我还使用了一些EditText中的图标作为它的可绘制内容:

<EditText
    android:id="@+id/passwordInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_lock"
    android:drawablePadding="@dimen/medium_margin_padding"
    android:hint="@string/password_text"
    android:inputType="textPassword" />

然而,我找不到可以用于在EditText中给可绘制对象着色的代码。这是不可能的吗?

注意:

我使用了 appcompat 和 design support library,但仍然找不到任何代码。

5个回答

40

使用DrawableCompat类wrap, setTint, setTintMode方法,以编程方式为可绘制对象设置色调。

Drawable drawable = getyourdrawablehere;
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.GREEN);
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

设置editText的drawable后:

editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);

3
PorterDuff.Mode更改为SRC_IN - cinfwatd

13

像这样使用位图标记创建可绘制对象:

drawable_with_tint.xml

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

</bitmap>

那么您可以在EditText中使用drawable。

<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_drawable_with_tint"
android:drawablePadding="@dimen/medium_margin_padding"
android:hint="@string/password_text"
android:inputType="textPassword" />

你是想将位图变得稍微透明一点吗? - Sheychan
我不使用任何tintMode。只是将图标颜色更改为其他颜色。通常我通过在ImageView中使用tint来实现这一点。 - Rendy
EditText中没有“android:tint”。 - Rendy
在drawable中的"android:tint" - Sheychan
哦,但它不起作用。无论如何,其他答案中的解决方案有效 :) - Rendy
显示剩余4条评论

13

很不幸,mbelsky发布的解决方案需要做一些非常小的更改。为了给你的EditText着色,你需要使用wrap setTintsetTintMode。适用于所有API的完整解决方案:

Drawable drawable = getResources().getDrawable(drawableID/mipmapID);
            emailDrawable = DrawableCompat.wrap(emailDrawable);
            DrawableCompat.setTint(emailDrawable,getResources().getColor(R.color.purple));
            DrawableCompat.setTintMode(emailDrawable, PorterDuff.Mode.SRC_IN);
            editText.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);

7
您可以使用

标签


android:drawableTint="@color/yourcolor"

注意,此功能仅适用于Api 23或更高版本。

3
希望Google Android团队能够添加API 23兼容性,以便我们可以摆脱像上面展示的写大量代码的麻烦! - Filipe Brito

0
在我的情况下,被接受的答案在5.0设备上无法正常工作,通过DrawableCompat对背景进行着色似乎可以在Button和其他资源上工作,但不能在EditText上工作。
我在与此主题斗争数小时后,在Android文档中发现了这个句子:androidx.appcompat.widget.AppCompatEditText

支持旧版本平台上的兼容功能的EditText,包括:

通过ViewCompat中的背景着色方法允许其背景的动态着色。

因此,对我而言,编程着色方法在API 21上可行,也适用于其他部分。
        ViewCompat.setBackgroundTintList(myEditText, ColorStateList.valueOf(getResources().getColor(R.color.my_color)));

请参考:AppCompatEditText


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