android - 设置 TextView 可绘制对象的颜色

标签 android xamarin xamarin.android

我试图在 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 中做这样的事情?

最佳答案

尝试以下解决方案

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

关于android - 设置 TextView 可绘制对象的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43369484/

相关文章:

android - 位图压缩给出空文件(0 字节)

c# - 我是否需要自己在 xamarin.forms 中清理内存?

android - 我如何解释这个 Android 堆栈跟踪?

visual-studio - Xamarin.Forms Android 应用程序在启动时崩溃(仅在发布版本中)

Xamarin Android 应用程序消耗异常大量的电池。为什么?

xamarin - 使用 Xamarin.Studio 构建 Xamarin.Android 项目时出错

安卓应用程序崩溃

android - 我可以使用 adb 更改默认启动程序吗?

php - 图像未显示在android中的imageview中

android - 使用 Xamarin 的 NDK Android 应用程序中的 DllNotFoundException