设置TextView Drawable的颜色

CDr*_*sos 8 android xamarin.android xamarin

我试图改变Xamarin中TextView Drawable的颜色.

在Java中你可以这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView txt = (TextView) findViewById(R.id.my_textview);
    setTextViewDrawableColor(txt, R.color.my_color);
}

private void setTextViewDrawableColor(TextView textView, int color) {
    for (Drawable drawable : textView.getCompoundDrawables()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(getColor(color), PorterDuff.Mode.SRC_IN));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Xamarin.Android中做这样的事情?

Gie*_*kas 21

我在 kotlin 中使用这个:

tv.getCompoundDrawables()[0].setTint(//color)
Run Code Online (Sandbox Code Playgroud)

  • 我会这样做: tv.getCompoundDrawables()[0]?.setTint(//color) (6认同)
  • 应该是答案! (2认同)

Nik*_*kiy 16

请注意,如果您通过android:drawableStartandroid:drawableEnd代替android:drawableLeftandroid:drawableRight分别在布局文件中设置可绘制对象,则应使用TextView.getCompoundDrawablesRelative()。否则,您可以获得空的可绘制数组。

private void setTextViewDrawableColor(TextView textView, int color) {
    for (Drawable drawable : textView.getCompoundDrawablesRelative()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!非常有趣,我在 RecyclerView 中进行着色,但它并不可靠。使用compoundDrawablesRelative 似乎没问题。 (2认同)

Art*_*out 12

尝试以下解决方案

private void setTextViewDrawableColor(TextView textView, int color) {
        for (Drawable drawable : textView.getCompoundDrawables()) {
            if (drawable != null) {
                drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 对于任何想要在 Java 中执行此操作的人:`yourTextView.getCompoundDrawables()[0].setTint(getResources().getColor(R.color.red));` (2认同)

Alg*_*gar 12

通过以下方式内置了对此的支持TextViewCompat.setCompoundDrawableTintList(textView, colors)

val color = ContextCompat.getColor(context, R.color.foo)
val colorList = ColorStateList.valueOf(color)
TextViewCompat.setCompoundDrawableTintList(textView, colorList)
Run Code Online (Sandbox Code Playgroud)


Yur*_*yac 10

// index of drawable
val left = 0
val start = left
val top = 1
val right = 2
val end = right
val bottm = 3

// color int
val color = Color.RED

// apply tint for target drawable
textView.compoundDrawables.getOrNull(left)?.setTint(color)

// apply tint for all drawables
textView.compoundDrawables?.forEach { it?.setTint(color) }
Run Code Online (Sandbox Code Playgroud)

笔记!

如果在 XML 布局中使用android:stratDrawableandroid:endDrawable必须使用textView.compoundDrawablesRelative数组,textView.compoundDrawables则在添加了android:leftDrawableandroid:rightDrawable属性时包含可绘制对象。


小智 5

我解决了这个问题,在 xml 定义中添加了这一行:

android:drawableTint="@color/red"

一个完整的例子:

        <TextView
            android:id="@+id/tv_element"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignParentEnd="true"
            android:drawableStart="@drawable/ic_icon"
            android:drawableTint="@color/color"
            android:visibility="visible" />
Run Code Online (Sandbox Code Playgroud)


Dev*_*aja 5

我遇到的问题是,我正在更改复合绘图的颜色,但所有颜色的其余部分都是不变的。对我来说很困惑。!!!

对我有用的解决方案是

// Pass through the each drawable and update the tint if drawable is set.
textView.compoundDrawables.filterNotNull().forEach { drawable ->
        drawable.mutate()
        drawable.setTint(drawableColor)
    }
Run Code Online (Sandbox Code Playgroud)

如果没有mutate(),一切都只能部分地进行。我在这里得到了更多细节Drawable Mutations

为了满足读者的兴趣,我在下面提供了一个简短的总结。

Android Drawables 是绘图容器。例如BitmapDrawable用于显示图像,ShapeDrawable用于显示形状和渐变。

Drawables are used extensively in the Android ecosystem thus they are optimized. So when views are created the different instances are spawned but the drawables associated with the view share the common state, called "Constant state".

例如,如果可绘制对象是 BitmapDrawable,则相同的位图将与所有相应的副本或视图一起使用。

优点:它可以节省大量内存。

问题:由于同一个绘图在不同的视图之间共享。可绘制对象状态的任何变化(例如 Alpha、变换等)都会影响所有使用它的地方。

解决方案:在可绘制对象上调用 mutate() 方法时,可复制可绘制对象的常量状态,以允许您更改任何属性而不影响其他可绘制对象。注意:位图仍然是共享的。