colors.xml not generating the correct values in ge

2019-08-23 06:44发布

问题:

I'm trying to access each of the values in colors.xml, but the int (hex) values in the R/color class doesn't match corresponding values that are defined in colors.xml. Here are some sample values:

 R/color (all of which starts with 0x7f0400...):
    public static final int AliceBlue=0x7f04002d;
    public static final int AntiqueWhite=0x7f040023;
    public static final int Aqua=0x7f04007d;
    public static final int Aquamarine=0x7f040062;
    public static final int Azure=0x7f04002b;
...

 But in colors.xml (all of which starts with #00...):
    <color name="AliceBlue">#F0F8FF</color>
    <color name="AntiqueWhite">#FAEBD7</color>
    <color name="Aqua">#00FFFF</color>
    <color name="Aquamarine">#7FFFD4</color>
    <color name="Azure">#F0FFFF</color>

回答1:

The values in the R file are not the values that you define they are the id that will be assigned to each color so you access them. You can use R.color.AliceBlue and it will give you the value you assigned to it in colors.xml. Do Not modify the R.java file. It is generated automatically.



回答2:

The number in your R.java (0x7f04002d) isn't suppose to correspond to your hex code in colors.xml (ex. F0F8FF). The int number in R.java is an identifier that the system generates when you compile your program.

When you access it with R.color.AliceBlue it will reference the int in R.java and should return the correct color.

You shouldn't even need/want to be messing around in R.java so don't worry about what's in there as long as it is returning the correct colors. Also note that you can't rely on that int because they can change from compile to compile so you don't want to reference that int. You just reference the name you gave it to refer to such as AntiqueWhite.

To reference this you need to get a Color by referencing its id using getColor()

new ForegroundColorSpan(getResources().getColor(AliceBlue));