在布局设置文字颜色在TextView中/ main.xml中主要布局文件中没有引用colors.xml文件。 (就是了#RRGGBB代替@色/ text_color)布局、文件、颜色、文字

2023-09-05 10:01:20 作者:忧郁的小蓝猫

我想设定一些一般的色彩,为节目我写。我创建了一个colors.xml文件,我试图从layout.xml文件中直接引用的颜色。我相信我在做这个正确但它给我以下错误:

I'm trying to set some general colors for a program I'm writing. I created a colors.xml file and am trying to directly reference the colors from the layout.xml file. I believe I'm am doing this correctly however it's giving me the following error:

Color value '@colors/text_color' must start with #

下面是我的RES /价值/ colors.xml

Here is my 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 /布局/ main.xml中

Here is my 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开发者网站的一些参考:更多资源类型:颜色< /一>,发现这个code:

I looked at some references on the android developers site: More Resource Types : Color and found this code:

例:保存在RES /价值/ colors.xml XML文件:

Example:XML file saved at res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
</resources>

该应用程序code检索颜色资源:

This application code retrieves the color resource:

Resources res = getResources();
int color = res.getColor(R.color.opaque_red);

这个布局XML应用颜色的属性:

This layout XML applies the color to an attribute:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

我觉得我的两个XML文件都遵循这个例子pretty的接近 - 但唯一不同的是,我没有使用任何应用程序code检索颜色资源。我不相信这是必要的(但它是有区别的。)我想我会看看别人有类似的问题或解决方案?或者这是一个错误?

I think my two xml files follow this example pretty close - however the only difference is that I haven't used any application code to retrieve the color resource. I don't believe this is necessary (but it is a difference.) I thought I'd see if anyone else had similar problems or a solution? or is this a bug?

我没有更新我的所有的Andr​​oid SDK(和Eclipse插件)上周文件,所以我相信他们是最新和最伟大的。

I did update all my android sdk (and Eclipse plugin) files last week so I believe them to be the latest and greatest.

-J

推荐答案

尝试在该案件后: 安卓文字颜色=@颜色/ text_color是错误的,因为 @Color 不依赖于文件名。您可以命名您的资源文件foobar.xml,这并不重要,但如果你在它定义了一些颜色则可以使用 @色/ some_color 访问它们。

After experimenting on that case: android:textColor="@colors/text_color" is wrong since @color is not filename dependant. You can name your resource file foobar.xml, it doesn't matter but if you have defined some colors in it you can access them using @color/some_color.

更新---- 从你的链接:

文件位置:   RES /价值/ colors.xml   文件名是任意的。元素的名称将被作为资源ID

file location: res/values/colors.xml The filename is arbitrary. The element's name will be used as the resource ID.