Home >Backend Development >XML/RSS Tutorial >Detailed introduction of Android color (color) code in XML files and java code
Android color (color) is in the XML file and java code, friends in need can refer to it.
1. Use constants of the Color class, such as:
int color = Color.BLUE;//创建一个蓝色 是使用Android提供的颜色 int color = Color.RED; int color = Color.WHITE;
2. Build through ARGB, such as:
int color = Color.argb(127, 255, 0, 255); //半透明的紫色其中第一个参数表示透明,0表示完全透明,255(ff)表示完全不透明;后三位分别代表RGB的值了。
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 For a color named mycolor, you can obtain the color value by referencing mycolor elsewhere, such as
textView definition:
Android:textColor="@drawable/mycolor"
Can be used in the ResourceManager class in Java code GetColor to get the color:
int color = getResources().getColor(R.color.mycolor);
This is the same value as the second method. The getResources() method returns the ResourceManager class instance of the current active Activity.
Note: The XML definition method accepts both 6-bit and 8-bit representations, and the beginning must be #. When 8-bit is defined, the first two digits indicate transparency. (For simplicity, it can also be in abbreviated form), for example:
<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. Directly define the color value, such as:
int color = 0xff00ff00;
This method must start with 0x instead of us common#. Unlike method 3, the value must also be represented by 8 bits, and 6-bit color representations are not accepted. Group them into 0x|ff|ff00ff, 0x is a mark representing a color integer, ff represents transparency, and ff00ff represents an RGB color value.
The above is the detailed content of Detailed introduction of Android color (color) code in XML files and java code. For more information, please follow other related articles on the PHP Chinese website!