我正在尝试为我正在编写的程序设置一些通用颜色。我创建了一个 colors.xml 文件,并试图直接从 layout.xml 文件中引用颜色。我相信我这样做是正确的,但是它给了我以下错误:
Color value '@colors/text_color' must start with #
这是我的 res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#888888</color>
<color name="text_color">#00FFFF</color>
</resources>
这是我的 res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:text="@string/hello"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
android:textColor="@colors/text_color"/>
</LinearLayout>
我查看了 android 开发者网站上的一些引用资料:More Resource Types : Color并找到了这段代码:
示例:保存在 res/values/colors.xml 中的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
此应用程序代码检索颜色资源:
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
此布局 XML 将颜色应用于属性:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/translucent_red"
android:text="Hello"/>
我认为我的两个 xml 文件非常接近此示例 - 但是唯一的区别是我没有使用任何应用程序代码来检索颜色资源。我不认为这是必要的(但这是有区别的。)我想我会看看其他人是否有类似的问题或解决方案?还是这是一个错误?
上周我确实更新了我所有的 android sdk(和 Eclipse 插件)文件,所以我相信它们是最新的。
最佳答案
仅使用标准颜色代码的变体:
android:textColor="#ff0000"
关于android - 在 layout/main.xml 主布局文件中的 TextView 中设置 textColor 不引用 colors.xml 文件。 (它想要一个#RRGGBB 而不是@color/text_color),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5286275/