Android Simple Development tutorial 22: Using Resource resources

Source: Internet
Author: User

In the previous example, we overlooked an important principle, in code and layout, to use string constants directly, such as:

<button android:text= "pattern"

Android:id= "@+id/btnpattern"

Android:layout_width= "Wrap_content"

Android:textcolor= "@color/black"

Android:checked= "true"

android:layout_height= "Wrap_content" >

</Button>

We directly define the button's display as "pattern". If you want your application to support multiple devices, multiple languages, the direct use of string constants can pose a big problem for porting programs. So design application is an important principle is to as far as possible the UI related resources (such as images, text, etc.) in the form of external resources to define.

Android supports a variety of resource types, and for each resource you can define a default resource and multiple optional resources (depending on the device configuration or language type, etc.).

The default resource is defined in relation to the device configuration and language and is used when the corresponding resource is not found with the device configuration. For example, you can define the default UI layout in Res/layout, while the screen horizontal (landscape) is defined in Res/layout-land. Android automatically chooses the right resources based on the device configuration at run time.

The following illustration shows that the default resource definitions are used when there are no optional resources defined in the two different configured devices:

The following figure applies a definition to two different devices that define two resources, one is the default resource, and the other is the resource that is displayed for the horizontal screen:

When defining optional resources, Android has a certain set of naming methods for optional resources, see http://developer.android.com/guide/topics/resources/providing-resources.html

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

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.