xamarin 设置TextView可绘制对象的颜色

ttygqcqt  于 2022-12-07  发布在  其他
关注(0)|答案(9)|浏览(213)

我正在尝试在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));
        }
    }
}

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

bybem2ql

bybem2ql1#

尝试以下解决方案

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

jbose2ul2#

我在Kotlin使用这个:

tv.getCompoundDrawables()[0].setTint(//color)
xfb7svmp

xfb7svmp3#

请注意,如果您在布局文件中通过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));
        }
    }
}
nwnhqdif

nwnhqdif4#

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

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

nwsw7zdq

nwsw7zdq5#

我通过在xml定义中添加以下行来解决这个问题:
"@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" />
ylamdve6

ylamdve66#

通过TextViewCompat.setCompoundDrawableTintList(textView, colors)提供内置支持

val color = ContextCompat.getColor(context, R.color.foo)
val colorList = ColorStateList.valueOf(color)
TextViewCompat.setCompoundDrawableTintList(textView, colorList)
htzpubme

htzpubme7#

对于Kotlin。使用TextView可绘制对象的以下扩展。它支持低于和高于API级别23。

private fun TextView.setTextViewDrawableColor(color: Int) {
        for (drawable in this.compoundDrawablesRelative) {
            drawable?.mutate()
            drawable?.colorFilter = PorterDuffColorFilter(
                color, PorterDuff.Mode.SRC_IN
            )
        }
    }

注意:您也可以在RecyclerView项目中使用此功能,它不会为每个项目override相同的颜色

s4n0splo

s4n0splo8#

如果要更改任何视图的可绘制对象的淡色颜色(在API 29上测试):

private fun setTintColor(textView: TextView, color: Int) {

    DrawableCompat.setTint(DrawableCompat.wrap(textView.background).mutate(), 
    ContextCompat.getColor(this, color))

}
hvvq6cgz

hvvq6cgz9#

我面临的问题是,我正在改变的颜色复合可绘制和除了一个其余的所有颜色是不变的。困惑我。!!!
对我有效的解决方案是

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

没有mutate(),事情是部分工作。我在这里得到了更多的细节可绘制的突变。
为了读者的兴趣,我在下面提供一个快速的总结。
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、转换等)都将影响使用它的所有地方。
  • 解决方案 *:在drawable上调用mutate()方法时,将复制drawable的常量状态,以便您可以更改任何属性而不影响其他drawable。注意:位图仍为共享。

相关问题