Color representation in Android
In Android, colors are represented by a 32-bit integer. The 32-bit integer contains 4 bytes, the first byte represents the transparency of the color (Alpha), 0 means completely transparent, and 0xFF means completely opaque. Bytes 2, 3, 4 represent the values of the three color components of the color in the RGB color space, red (R), green (G) and blue (B), respectively. 0 means that there is no color component, and 0xFF means that the color component reaches its maximum. For example, 0xCCFF0000 means 80% transparency red.
Color representation in XML
In XML, use # to add color value to represent a color, such as #FFA1A100. If the transparency is 0xFF, i.e., it is completely opaque, the transparency can be omitted, for example #FFA1A100 can be written as #A1A100.
Color representation in code
In the code, you can use getColor() to get the configured color in XML, or you can directly use an integer value of a color to represent the color.
It should be noted that when an integer value of a color is directly used to represent the color, the transparency cannot be omitted when it is 0xFF, otherwise the transparency will be considered to be 0, which means it is completely transparent, which will cause the color to be unable to be displayed.
For example, the color #A1A100 in XML should be written as 0xFFA1A100 in the code, but not 0xA1A100.
For example, to set a red dividing line for listView, the correct way to write it is
(new ColorDrawable(0xFFFF0000)); (1);
If written
(new ColorDrawable(0xFF0000)); (1);
Then there is no dividing line, because 0xFF0000 represents a completely transparent color.
This can also be seen when getting the color configured in the XML through getColor().
For example, the following color is defined in xml
<color name="color_in_name">#A1A100</color>
Get the color in the code
int color = getResources().getColor(.color_in_name); ("color value: ", (color));
You can see that the actual value of color is -6184704, that is, 0xFFA1A100, not 0xA1A100.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!