Android Application Resources-Providing Resources (1)

Source: Internet
Author: User

Resources such as images and strings should always be separated as external resources and code, so that they can be maintained independently. You can also organize resources to the Resource Directory named in a special name to provide optional resources for special device configurations. At runtime, the Android system selects appropriate resources based on the current configuration. For example, you can provide different UI la s based on different screen sizes, or provide strings of different languages based on system language settings.

After separating application resources as external resources, you can use the resource ID of the automatically generated R class in the project to access the corresponding resources. How to use resources in an application will be discussed in detail in the "access resources" section. This document mainly discusses how to organize resources into an application, and provides optional resources for special device configurations.

Group by resource type

Each type of resource should be put in a specific subdirectory of the res/directory of the project. For example, the following is a simple project's file structure:

MyProject/

Src/

MyActivity. java

Res/

Drawable/

Icon.png

Layout/

Main. xml

Info. xml

Values/

Strings. xml

In the preceding example, the res/directory contains all resources (in its subdirectories): one image resource, two layout resources, and one string resource file. The resource directory name is important. The following table 1 describes the Resource Directory names:

 

Directory Resource Type
Animator/ XML file for storing property animation Definitions
Anim/ Store the XML file that defines the makeup animation (the XML file of the property animation can also be saved in this directory, but the animator/directory is dedicated to the property animation, to differentiate the two animation types .)
Color/ XML file for storing the defined color value
Drawable/ Save the position image file (.png).jpg).gif).9.png), or compile it into the following XML file that can be traced to the resource type:
1. Bitmap File
2. variable size bitmap (Nine-Patches)
3. Status image (states list)
4. Shape
5. animation Rendering
6. Other plotting Resources
Layout/ Store XML files that define the user interface Layout
Menu/ Stores XML files that define Application menus, such as option menus, context menus, or submenus.
Raw/ Stores files in any native format. Call Resources. openRawResource () to open these Resources.
However, to access the original file names and directories, you need to save them to the assets/directory instead of the res/raw/directory ). Files in the assets/directory do not have resource IDs, so they can only be read using AssetManager.
Values/ Stores XML files containing simple values, such as strings, integers, and colors.
XML resource files in other res/subdirectories define a resource based on the XML file name, while files in the values/directory can define multiple types of resources. The sub-elements of each <resources> element in each file in this directory define a resource. For example, the <string> element creates the R. string resource and the <color> element creates the R. color Resource.
Because each type of resource is defined by their own XML elements, there is no file name restriction and different resource types can be put in a file. However, to be clear, you may want to put only one type of resource in a file. The following is a convention for creating a resource file name in this directory:
Arrays. xml: Resource array (array type)
Colors. xml: For color values
Dimens. xml: dimension value
Strings. xml: String Value
Styles. xml: Style
Xml/ Any XML file stored in this directory can be read at runtime by calling the Resources. getXML () method. Various XML configuration files must be saved here, such as searchable configuration files.
 
Warning do not store resource files directly in the res/directory, which may cause compilation errors.
The resource is the default resource in the subdirectory defined in Table 1. That is to say, these resources define the default design and content of the application. However, different types of Android devices can call different types of resources. For example, if a device's screen is larger than a normal screen, different layout resources should be provided to meet the needs of additional screen space. Alternatively, if the device has different language settings, different string resources should be provided to display the text that matches the device language on the user interface. In addition to the default resources, you must provide alternative resources for different device configurations.
 

From FireOfStar's column

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.