How Android uses the color defined by the resource file

Source: Internet
Author: User
Tags getcolor unique id

There are two ways to use a resource file, one is to refer to it in XML, and one to refer to it in code.

For example, we define the following two colors in XML, which are quoted in the XML file, very simply, using @color/drakblue.

 <  resources  >  <  color  name  = "Weakyellow"  >  #ee9c16 </ color     >  <  color  name  = "Darkblue"  >  #445f74 </ color  >  </ resources  >  

But what if you reference it in your code?

I have been for quite a long time did not reference the resource file in the code, today occasionally quoted, unexpectedly tried for a period of time, before the color tune normal.

At first I was defining this:

colors = new Integer[]{color.cyan,color.dkgray,color.blue,color.green,color.ltgray,color.magenta,color.black, Color.red,color.yellow,color.white,r.color.weakyellow,r.color.darkblue};

In front of which is the system comes with color, because the system itself provides 10 colors, to the 11th and 12 kinds of time, can only be defined by themselves, so there is the front I define the two colors, write the array, let its natural imitation of the previous form of a:

R.clor.darkblue

Looks like the form is also pretty well, when the test found that no matter how, the latter two colors show a solid color, completely unrelated to my definition.

How can we define that? Completely forget the basic concept, what does "r.clor.darkblue" mean? The students?
It represents the resource value, and the contents of each resources will generate a unique ID value in the R file, which has nothing to do with color!
As an example, use
Textview.settext (r.string.me)

Is that right? Inside is the int value, where is the string yo you want!

Well, the right way should be familiar with this kind of drip ... Goodbye ~

colors = new Integer[]{color.cyan,color.dkgray,color.blue,color.green,color.ltgray,color.magenta,color.black, Color.red,color.yellow,color.white,getresources (). GetColor (R.color.weakyellow), Getresources (). GetColor ( R.color.darkblue)};
Getresources (). GetColor (R.color.darkblue)

How Android uses the color defined by the resource file

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.