TextView Drawable 兼容返回 null

Fai*_*sal 2 android textview

我的 TextView 有与其关联的 Drawable 图标,如下所示:

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvProfile"
    android:drawableStart="@drawable/ic_menu_profile" />
Run Code Online (Sandbox Code Playgroud)

当尝试在运行时对 textView 的可绘制紧凑进行着色/着色时,以下尝试返回 null。但是,android:drawableLeft返回可绘制实例。

tvProfile.compoundDrawables[0]?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
Run Code Online (Sandbox Code Playgroud)

想知道有什么解决方案吗android:drawableStart

Gab*_*tti 5

使用compoundDrawablesRelative方法:

tvProfile.compoundDrawablesRelative[0]?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
Run Code Online (Sandbox Code Playgroud)

您可以检查文档

start返回、topend和边框的可绘制对象bottom

而返回、、和边框getCompoundDrawables的可绘制对象。lefttoprightbottom