Setting textColor to TextView in the layout /main.xml is the main layout file, not referencing the colors.xml file. (He wants #RRGGBB instead of @ color / text_color)

I am trying to set some common colors for the program I am writing. I created the colors.xml file and am trying to directly refer to the colors from the layout.xml file. I believe that I am doing it right, but it gives me the following error:

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

Here are 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> 

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 links on the Android developer site: Additional resource types: Color and found this code:

Example: XML file saved in 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 a color resource:

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

This XML layout applies color to the attribute:

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

I think that my two xml files follow this example quite closely - however, the only difference is that I did not use the application code to extract the color resource. I don’t think it’s necessary (but that’s the difference.) I thought I would see if anyone else has any problems or solution? or is it a mistake?

I updated all my Android sdk (and Eclipse) files last week, so I think they were the last.

+42
android
Mar 12 '11 at 23:23
source share
4 answers

After the experiment, in this case: android:textColor="@colors/text_color" is incorrect, since @color is independent of the file name. You can name your resource file foobar.xml, it doesn’t matter, but if you defined some colors in it, you can access them with @color/some_color .

Update:

file location: RES / values ​​/ colors.xml The file name is arbitrary. The item name will be used as the resource identifier. ( Source )

+28
Mar 12 '11 at 23:52
source share

Option using only the standard color code:

 android:textColor="#ff0000" 
+37
Mar 14 '14 at 17:32
source share

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

 android:textColor="@color/text_color" 

that "@color" without 's'.

+15
Mar 13 '11 at 1:10
source share

You should write textcolor in xml as

 android:textColor="@color/text_color" 

or

 android:textColor="#FFFFFF" 
+2
02 Mar. '17 at 10:10
source share



All Articles