【问题标题】:EditText Drawable Tint not possible?EditText Drawable Tint 不可能?
【发布时间】:2020-04-21 17:53:06
【问题描述】:

我为整个应用程序的图标使用色调。

我的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 中找不到任何可用于为drawable 着色的代码。不能给drawable着色吗?

注意:

我使用了 appcompat 和设计支持库,但仍然找不到任何代码。

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    使用DrawableCompat class 中的wrap, setTint, setTintMode 方法以编程方式为drawable 设置色调

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

    【讨论】:

    • PorterDuff.Mode 更改为SRC_IN
    【解决方案2】:

    用这样的位图标签创建一个drawable

    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" />
    

    【讨论】:

    • 你想让位图有点半透明吗?
    • 我不使用任何 tintMode。没有唯一的改变图标颜色到其他。通常我通过在 ImageView 中使用 tint 来实现。
    • EditText 中没有“android:tint”
    • "android:tint" in the drawable
    • 哦,但它不起作用。无论如何,其他答案中的解决方案有效:)
    【解决方案3】:

    不幸的是,mbelsky 发布的解决方案需要一些非常小的改动。为了在 EditText 中为 drawable 着色,您需要使用 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);
    

    【讨论】:

      【解决方案4】:

      你可以使用

      android:drawableTint="@color/yourcolor"
      

      请注意,它仅适用于 Api 23 或更高版本

      【讨论】:

      • 希望谷歌 Android 团队添加对 API 23- 的兼容性,让我们免于编写如上所示的大量代码!!!
      【解决方案5】:

      在我的情况下,接受的答案不适用于 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-23
        • 2012-06-09
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多