Android .xml files: Why do predefined colors not work for me?
When I look at misc. Android tutorials and examples when it comes to specifying colors I very often see constants like @color/red
or @color/black
etc. being used. For some strange reason that NEVER works for me! I always need to specify colors using the "#RGB", #ARGB, ..., #AARRGGBB notation.
As soon, as I try to use any of those mnemonic constants like e.g. "@color/red" I am getting error messages like these:
[...] C:\Users\mmo\Test\res\drawable\edit_text.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/orange').
[...] C:\Users\mmo\T开发者_开发百科est\res\drawable\myDrawable.xml:3: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:4: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:5: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/green').
[...] C:\Users\mmo\Test\res\drawable\myDrawable.xml:6: error: Error: No resource found that matches the given name (at 'drawable' with value '@color/black').
Why is that so? Why can't I use these predefined constants? Do I need to prefix them with some package name (I tried @android:color/red
but that only caused a different error)?
Do I need to specify these colors myself? If so: how and where? Any ideas or suggestions?
Michael
If you want to use the colors pre-defined in the Android platform, the syntax is @android:color/white
. The android:
at the beginning indicates that the resource is not part of your application.
Is "colors.xml" added to your res/values folder where these color constants are defined?
Color XML file is within the values folder where it must contain color values.within resources tag.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="green">#00ff00</color>
Make sure your color XML file is within the values folder, not a colors folder.
So you should have...
values/colors.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="red">#FF0000</color>
</resources>
and NOT this...
color/colors.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<color name="red">#FF0000</color>
</selector>
Note that the tag is resources, not selector.
strangely Android does not provide a decent list of colors. And I say strangely because during my 30 years career this is the first language I met that does not do that. And that despite that is built on Java which defines colors in all it's basic libraries.
The ones that are defined are prefixed so you will not find them :) To find them (if using eclipse ) go to the xml doc where you need the color type android:background="@android:color/ and do a Ctrl Space. On my version (current as we speaking) I get more than a dozen. for instance: holo_orange_dark
So, use that or complain so Google fixes this issue. And I call it issue because it makes no sense to force all developers to manually describe all colors and values.
An important part of this that no one else has mentioned is that the reference to the color has to be
@color/black
but the xml file has to be
colors.xml
(note plural in the xml file name but not plural @color)
精彩评论