<Android> resource access, color, string, size, XML, drawables Resource Usage

Source: Internet
Author: User

1. Access to resources

The code uses the Getresources () method of the context to get the resources object, access its own defined resource R. resource file type. resource file name, Access system-defined resource ANDROID.R. Resource file type. resource file name.

Referencing resources in other resources The general format is @[package name:] Resource type/resource name

android:textcolor= "@color/opaque_red"

2. Use of color resources

1. Definition of color resource XML

 <?XML version= "1.0" encoding= "Utf-8"?> <Resources>    <Colorname= "RED_BG">#f00</Color>    <Colorname= "Blue_text">#0000ff</Color> </Resources>

2. Referencing color resources in other XML resources

android:textcolor= "@color/blue_text"

referencing color files in 3.java code

Referencing a color resource, setting the background color to red

1 getWindow (). Setbackgrounddrawableresource (R.COLOR.RED_BG);

The method of obtaining color Resources.getcolor ();

3. Use of string resources

1. Definition of String XML

 <?XML version= "1.0" encoding= "Utf-8"?> <Resources>    <stringname= "App_name">Test Resources</string>     <stringname= "TEST_STR1">Reference from the code!</string>     <stringname= "TEST_STR2">Referencing from a resource file!</string> </Resources>

2. References in other XML files

android:text= "@string/test_str1"

Referenced in 3.java code

String str = getString (r.string.test_str2). toString (); // Use the Context.getstring () method to pass the resource ID parameter to get the string. 

Supplemental Get string Resource Method Resources.getstring (), Java code r.string.string_name with color resources

4. Use of size resources

The size of Android is px,in,mm,pt (dots, feet in 1/72), DP,SP

1. Definition of dimension Resources

 <?XML version= "1.0" encoding= "Utf-8"?> <Resources>     <dimenname= "Text_width">150px</dimen>     <dimenname= "Text_height">100px</dimen>     <dimenname= "Btn_width">30mm</dimen>     <dimenname= "Btn_height">10mm</dimen> </Resources>

2. Reference dimensions in other XML resources (set TextView width and height)

Android:width= "@dimen/text_width" android:height= "@dimen/text_height"

3. Reference dimensions in Java code (set button width and height)

Resources r = getresources (); float btn_h = r.getdimension (r.dimen.btn_height); float btn_w = r.getdimension (r.dimen.btn_width); Mybutton.setheight ((int) btn_h); Mybutton.setwidth ((int) btn_w);

Supplementary Java code can reference dimensions by R.dimen.dimen_name.

5. Use of raw XML resources

1. The definition of the original XML file Test.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>   <Customername= "Tom" Age= " the"Gender= "Male"Email= "[email protected]"/>   <Customername= "Kite" Age= "+"Gender= "Male"Email= "[email protected]"/></Resources>

2. Parsing the original XML file in Java code

intCounter = 0; StringBuilder SB=NewStringBuilder (""); Resources R=getresources (); Xmlresourceparser XRP=R.getxml (r.xml.test);
Try { while(Xrp.geteventtype ()! =xmlresourceparser.end_document) { if(Xrp.geteventtype () = =Xmlresourceparser.start_tag) {String name=Xrp.getname (); if(Name.equals ("Customer") ) {counter++; Sb.append ("section" +counter+ "Customer Information:" + "\ n"); Sb.append (Xrp.getattributevalue (0) + "\ n"); Sb.append (Xrp.getattributevalue (1) + "\ n"); Sb.append (Xrp.getattributevalue (2) + "\ n"); Sb.append (Xrp.getattributevalue (3) + "\ n"); } } Else if(Xrp.geteventtype () = =Xmlpullparser.end_tag) { } Else if(Xrp.geteventtype () = =xmlpullparser.text) {} xrp.next (); } mytextview.settext (Sb.tostring ());} Catch(xmlpullparserexception e) {e.printstacktrace ();}Catch(IOException e) {e.printstacktrace ();}

Supplement seems to only reference the original XML file resource in Java code

6. Use of drawables Resources

Drawables resources are divided into bitmap (bitmap pieces), Color drawable (color), Nine-patch image (Nine pictures) three class

When using bitmap files in XML, reference g1.jpg

android:src= "@drawable/g1"

When referencing a bitmap file in Java code

Resources r == r.getdrawable (r.drawable.moto); myimageview.setimagedrawable (d);

<Android> resource access, color, string, size, XML, drawables Resource Usage

Related Article

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.