Android Foundation 5-use resources

Source: Internet
Author: User

Android selects the most appropriate value for a resource identifier based on the current hardware, device, and language configuration. 1. Use static class R to access resources in code. R classes are generated based on external resources and created during project compilation. Each subclass of R represents its related resources as variables, such as R. string. app_name. However, it is only an integer that represents the position of the resource in the resource table. You also need to use constructors or methods similar to setContentView to pass resource variables. When you need an instance of the resource itself, you need to use an auxiliary method to extract them from the resource table. The Resources class contains getter for each available resource type. It is usually used by passing the ID of the resource instance you need. For example:

<Span style = "white-space: pre"> </span> Resources myResource = getResources (); <span style = "white-space: pre "> </span> String [] stringarray = myResource. getStringArray (R. array. string_array );

 

2. Referencing resources within a resource is very suitable for layout and style. Reference a resource and use it as the property value of other XML resources. With the @ symbol, You can reference another resource in one resource.
<Span style = "white-space: pre"> </span> attribute = "@ [packagename:] resourcetype/resourceidentifier ";

 

3. Use system resources to use system resources in code using the android. R class. To access system resources in XML, you must specify android as the package name, for example, android: text = "@ android: string/httpErrorBadUrl ";

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.