Androidcolor (color) code details in XML files and java code

Source: Internet
Author: User
Tags bit definition
Androidcolor (color) is in the XML file and java code. if you need it, refer to it. Android color is in the XML file and java code. For more information, see.

1. use constants of the Color class, such:

Int color = Color. BLUE; // create a BLUE color that is provided by Android. int Color = color. RED; int Color = color. WHITE;


2. build with ARGB, such:

Int color = Color. argb (127,255, 0,255); // translucent purple. The first parameter indicates transparent, 0 indicates completely transparent, and 255 (ff) indicates completely opaque; the last three digits represent the RGB values.


3. use an XML Resource file to define the color
This method is highly scalable and easy to modify and share. for example, you can create a color. xml file in the values Directory:

 
 
  
   #7fff00ff
  
 

Defines a color named mycolor. in other places, you can reference mycolor to obtain the color value, as shown in

TextView definition:

Android:textColor="@drawable/mycolor"

In Java code, you can use the getColor in the ResourceManager class to obtain the color:

int color = getResources().getColor(R.color.mycolor);

This is the same as the value obtained in the second method. the getResources () method returns the ResourceManager class instance of the current Activity.

Note: The XML definition method can be 6-bit or 8-bit, and must start with #. The first two digits of the 8-bit definition indicate transparency. (For simplicity, it can also be abbreviated), for example:

 
  #f00
 
 
  #0000ff
 
 
  #f0f0
 
 
  #ffffff00
 

4. define the color value directly, for example:

int color = 0xff00ff00;

This method must start with 0x instead #. Unlike method 3, the value must also be 8 bits, and 6 bits are not allowed. Group 0x | ff | ff00ff. 0x indicates the color integer, ff indicates the transparency, and ff00ff indicates the RGB color value.

The above is a detailed description of the code of Android color in the XML file and java code. For more information, see other related articles in the first PHP community!

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.