【问题标题】:Failed to convert colour into a drawable无法将颜色转换为可绘制对象
【发布时间】:2023-04-02 18:36:01
【问题描述】:

所以我对 android 编程非常陌生,我有一个我完全无法解决的问题。

我想让我的文本背景变成我在基础教程中学习的红色。 但是,问题是当我更改背景颜色时,出现错误:

“渲染问题无法将红色转换为可绘制对象”

这是我在activity_main中编写的代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:background="red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是我的 colours.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
<color name="Red">#E60000</color>
</resources>`

我什至尝试将第 10 行更改为 android:background="@color/redandroid:background="color/red 甚至android:background="@android:color/red,但似乎没有任何效果。

有人能帮我解决这个问题吗?

【问题讨论】:

  • 试试这个android:background="#f00
  • 也许你和*.com/questions/26618785/…有同样的问题
  • color name="Red" 尝试使用color name="red"。资源名称区分大小写。并且必须全部小写。

标签: android xml colors background render


【解决方案1】:

@color/red 仅在您在 colors.xml 文件(位于 res/values 下)中定义了红色资源时才有效

如果您不想将颜色值输入到您的 colors.xml 文件中,请尝试将 android:background 的值更改为红色的十六进制值,例如 #ff0000。

【讨论】:

    【解决方案2】:

    有很多方法可以做到...

    1.-创建一个名为color.xmlValue Resource File,然后在其中添加它

    <color name="red">#ffff0000</color>
    

    2.-现在将您的 TextView 更改为此并且应该可以工作。

    <TextView
    android:text="@string/hello_world"
    android:background="@color/red"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    

    如果你想通过代码获得那种颜色,你必须使用

    getResources().getColor(R.color.red)
    

    或者你也可以像@DerGolem说的那样声明你的颜色

    <TextView
    android:text="@string/hello_world"
    android:background="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    

    【讨论】:

    • 这很有魅力,非常感谢!解释得很好!
    • @carlsaptarshi 你为什么把这个问题删除为可接受的?
    • 我做到了?我不知道我有!???完全是偶然的,我的错
    【解决方案3】:

    添加文件 colors.xml(在 res/values 中):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <color name="red">#FF0000</color>
    </resources>
    

    `

    【讨论】:

      【解决方案4】:

      你的代码应该是这样的:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
      
      <TextView
          android:text="@string/hello_world"
          android:background="@color/Red"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
      

      由于您使用一些资源文件来声明颜色,因此您需要添加“@color/”以让操作系统知道您正在引用资源文件。

      然后在您的 color.xml 中,您将颜色命名为“红色”,但在活动文件中您将 ID 称为“红色”,请注意 ID 区分大小写。所以你在正确的轨道上,你唯一的错误是在资源文件中有一个“R”,而在背景颜色变化中有一个“r”;)

      希望对你有帮助

      编辑:

      你也可以直接使用android的OS Color,和Id一样可以达到,只是把Id部分换成Color,See Doc here

      【讨论】: