【问题标题】:How do I get the background color of a TextView?如何获取 TextView 的背景颜色?
【发布时间】:2013-06-20 21:31:27
【问题描述】:

如何获取 TextView 的背景颜色?

当我按下TextView时,我想根据正在使用的背景颜色来改变背景颜色。

TextView 没有这样的方法:

getBackgroundResource()

编辑: 我更喜欢获取背景颜色的 resId。

【问题讨论】:

  • 或者我在网上搜索了一下,但似乎没有办法从 xml 定义的颜色中获取这样的 id。可能您应该更改您的应用程序并以编程方式管理背景颜色,也许在 onClick 事件期间跟踪颜色更改。
  • 给定视图使用的颜色可能没有资源 ID。背景的res ID是R.attr.background,对应你的主题或风格中的<item name="android:background">。你可以get the color value from the theme,和TextView的颜色对比一下看是否匹配。

标签: android textview


【解决方案1】:

如果你想得到背景的颜色代码试试这个:

if (textView.getBackground() instanceof ColorDrawable) {
    ColorDrawable cd = (ColorDrawable) textView.getBackground();
    int colorCode = cd.getColor();
}

【讨论】:

  • 我编辑我的问题:我如何从中得到颜色的 resId?
  • 我试过这个,但在你回答的第一行之后,我得到 cd is null
  • 如何设置 TextView 的背景?要使用此代码,它必须是 Color,而不是 Gradient 或其他类型的 Drawable
  • 这是因为我在第一次出现时没有设置背景。我修好了,但现在 cd.getColor() 返回我 -1
  • 小心:这会抛出一个Exception:java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable
【解决方案2】:

如果您使用的是AppCompat,请使用:

ViewCompat.getBackgroundTintList(textView).getDefaultColor();

旁注:如果你转换为ColorDrawable,请小心,因为它可以抛出ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable

【讨论】:

    【解决方案3】:

    在 Kotlin 中:

        val cd = view.background as ColorDrawable
        val colorCode = cd.color
    

    【讨论】:

      【解决方案4】:

      回答:

      我们不能使用像 color.red 或 color.white 这样的常量。

      我们需要弄清楚如何

      int intID = (ColorDrawable) holder.tvChoose.getBackground().getColor();
      

      表示它,我们有颜色的假 ID

      【讨论】:

        【解决方案5】:

        ColorDrawable.getColor() 仅适用于 11 以上的 API 级别,因此您可以使用此代码从一开始就支持它。 我正在使用低于 API 级别 11 的反射。

         public static int getBackgroundColor(TextView textView) {
                Drawable drawable = textView.getBackground();
                if (drawable instanceof ColorDrawable) {
                    ColorDrawable colorDrawable = (ColorDrawable) drawable;
                    if (Build.VERSION.SDK_INT >= 11) {
                        return colorDrawable.getColor();
                    }
                    try {
                        Field field = colorDrawable.getClass().getDeclaredField("mState");
                        field.setAccessible(true);
                        Object object = field.get(colorDrawable);
                        field = object.getClass().getDeclaredField("mUseColor");
                        field.setAccessible(true);
                        return field.getInt(object);
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                return 0;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-11
          • 1970-01-01
          • 1970-01-01
          • 2015-12-17
          • 1970-01-01
          • 2015-07-22
          • 1970-01-01
          相关资源
          最近更新 更多