Use of colors in Android

Source: Internet
Author: User
Tags getcolor

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:

    1. Button btn1 = (button) Findviewbyid (R.id.button1);
    2. int color = Resources.getsystem (). GetColor (r.color.red);
    3. Btn1.setbackgroundcolor (color);


1.2.2 using colors in Layout files
@[Package:]color/color_name
For example:

    1. <button
    2. Android:id= "@+id/button1"
    3. android:layout_height= "Wrap_content"
    4. Android:layout_width= "Match_parent"
    5. android:text= "Address book"
    6. android:background= "@color/red"
    7. ></Button>

This place can also use color values directly, but it is not recommended.

    1. <button
    2. Android:id= "@+id/button1"
    3. android:layout_height= "Wrap_content"
    4. Android:layout_width= "Match_parent"
    5. android:text= "Address book"
    6. Android:background= "#ff0000"
    7. ></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);

    1. int color = Resources.getsystem (). GetColor (Android. R.color.background_dark);
    2. Btn1.setbackgroundcolor (color);

Color constants in the color class

    1. Button btn1 = (button) Findviewbyid (R.id.button1);
    2. Btn1.setbackgroundcolor (Color.cyan);

Using static methods in the Color class

    1. Button btn1 = (button) Findviewbyid (R.id.button1);
    2. Btn1.setbackgroundcolor (Color.argb (0xFF, 0xFF, 0x00, 0x00));

2.1.2 using system colors in Layout files

    1. <button
    2. Android:id= "@+id/button1"
    3. android:layout_height= "Wrap_content"
    4. Android:layout_width= "Match_parent"
    5. android:text= "Address book"
    6. Android:background= "@android: Color/background_dark"
    7. ></Button>

Use of colors in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.