Detailed introduction of Android color (color) code in XML files and java code

黄舟
Release: 2017-03-08 16:13:00
Original
4095 people have browsed it

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;
Copy after login


2. Build through ARGB, such as:

int color = Color.argb(127, 255, 0, 255);
//半透明的紫色其中第一个参数表示透明,0表示完全透明,255(ff)表示完全不透明;后三位分别代表RGB的值了。
Copy after login


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>
Copy after login

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"
Copy after login

Can be used in the ResourceManager class in Java code GetColor to get the color:

int color = getResources().getColor(R.color.mycolor);
Copy after login

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>
Copy after login

4. Directly define the color value, such as:

int color = 0xff00ff00;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!