Android color (color) in XML file and java code

Android color (color) In the XML file and java code, friends in need can refer to it.

1. Use the constants of the Color class, such as:

int color = Color.BLUE;//Create a blue color using the color provided by Android

int color = Color.RED; int color = Color.WHITE;

2. Build through ARGB , such as:

int color = Color.argb(127, 255, 0, 255);

//Translucent purple The first parameter represents transparency, 0 represents complete transparency, and 255(ff) represents complete opacity; the last three represent the RGB values ​​respectively.

3. Use XML resource files to define colors
. This method has good scalability and is easy to modify and share. For example, create a color.xml in the values ​​directory:

<?xml version=”1.0” encoding=”utf-8”>

<resources>

<color name=”mycolor”>#7fff00ff</color>

</resources>
defines a color named mycolor, which can be obtained by referencing mycolor elsewhere, such as

In the definition of textView: Android:textColor="@drawable/mycolor"

In Java code, you can use getColor in the ResourceManager class to get the color:
int color = getResources().getColor(R.color.mycolor);

This is the same as the value obtained by the second method. The getResources() method returns an instance of the ResourceManager class of the currently active Activity.

Note: The XML definition method accepts both 6-bit and 8-bit representations, and the beginning must be #, and the first two digits are transparent when 8-bit is defined. (Also abbreviated for simplicity), eg:
<color name="solid_red">#f00</color>
<color name="solid_blue">#0000ff</color>
<color name="solid_green">#f0f0 </color>
<color name="solid_yellow">#ffffff00</color>

4. Define the color value directly, such as:

int color = 0xff00ff00;

This method must start with 0x, instead of our usual #. Unlike method 3, the value must also be represented in 8 bits, and a 6-bit color representation is not accepted. Group 0x|ff|ff00ff, 0x is the mark representing the color integer, ff is the transparency, and ff00ff is the RGB color value.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941904&siteId=291194637