Colors used in development can be divided into two kinds, custom colors and system colors
1. Custom Colors:
The definition of a color value is defined by the RGB primary color and an alpha value (the additive principle). Start with the pound sign (#), followed by the alpha-red-green-blue format.
Shaped like:
#RGB
#ARGB
#RRGGBB
#AARRGGBB
Usually the form of #rrggbb or #aarrggbb is used. Where AA is transparency, FF is opaque, and 00 is transparent.
1.1 Define colors in the resource file:
The Colors.xml file is usually created under Res\values, and the color is defined as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resourses>
<colorname= "Red">#ff0000</color>
</resourses>
1.2 Use of colors
1.2.1 Using colors in your code
R.color.color_name
For example:
- Button btn1 = (button) Findviewbyid (R.id.button1);
- int color = Resources.getsystem (). GetColor (r.color.red);
- Btn1.setbackgroundcolor (color);
1.2.2 using colors in Layout files
@[Package:]color/color_name
For example:
- <button
- Android:id= "@+id/button1"
- android:layout_height= "Wrap_content"
- Android:layout_width= "Match_parent"
- android:text= "Address book"
- android:background= "@color/red"
- ></Button>
This place can also use color values directly, but it is not recommended.
- <button
- Android:id= "@+id/button1"
- android:layout_height= "Wrap_content"
- Android:layout_width= "Match_parent"
- android:text= "Address book"
- Android:background= "#ff0000"
- ></Button>
2. System color
Android also has some built-in colors, such as the colors defined in system resources, which are very limited.
Some color constants and static methods for constructing color values are also available in the Android.graphics.Color class.
2.1 Use of system colors
2.1.1 Using system colors in your code
The color values defined in system resources are very limited
Button btn1 = (button) Findviewbyid (R.id.button1);
- int color = Resources.getsystem (). GetColor (Android. R.color.background_dark);
- Btn1.setbackgroundcolor (color);
Color constants in the color class
- Button btn1 = (button) Findviewbyid (R.id.button1);
- Btn1.setbackgroundcolor (Color.cyan);
Using static methods in the Color class
- Button btn1 = (button) Findviewbyid (R.id.button1);
- Btn1.setbackgroundcolor (Color.argb (0xFF, 0xFF, 0x00, 0x00));
2.1.2 using system colors in Layout files
- <button
- Android:id= "@+id/button1"
- android:layout_height= "Wrap_content"
- Android:layout_width= "Match_parent"
- android:text= "Address book"
- Android:background= "@android: Color/background_dark"
- ></Button>
Use of colors in Android