开发者

setting textColor in TextView in layout/main.xml main layout file not referencing colors.xml file. (It wants a #RRGGBB instead of @color/text_color)

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 #

Here is my res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background_color">#888888</color>
    <col开发者_开发问答or name="text_color">#00FFFF</color>
</resources>

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>

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

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>

This application code retrieves the color resource:

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

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"/>

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?

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


A variation using just standard color code:

android:textColor="#ff0000"


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.

Update:

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


You have a typo in your xml; it should be:

android:textColor="@color/text_color"

that's "@color" without the 's'.


You should write textcolor in xml as

android:textColor="@color/text_color"

or

android:textColor="#FFFFFF"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜