Providing resources
You shoshould always externalize application resources such as images and strings from your code, so that you can maintain them independently. You shoshould also provide alternative resources for specific
Device configurations, by grouping them in specially-named resource directories. at runtime, Android uses the appropriate resource based on the current configuration. for example, you might want to provide a different UI layout depending on the screen
Size or different strings depending on the language setting.
Once you externalize your application resources, you can access them using resource IDs that are generated in your project'sR
Class. How to use resources in your application is discussed
In accessing resources. This document shows you how to group your resources in your android
Project and provide alternative resources for specific device configurations.
When we provide resources for apps, such as slices or strings, we can separate them and provide different resources for special device configurations, put them in the special named res directory. At runtime, Android will call the appropriate res according to the current configuration. For example, you may want to provide different UIS based on different screen sizes, or provide different strings based on the language.
When you call these resources, you can generate IDS through the R file. If you want to know how to use res, you can learn about accessing resources. This article describes how Android prepares resources for different devices.
Grouping resource types
You shoshould place each type of resource in a specific subdirectory of your project'sres/
Directory. For example, here's the File hierarchy for a simple project:
We should put different types of resources under different sub-directories of res, which is a simple res directory structure.
Project Res/The following resource directories are supported:
Directory |
Resource Type |
animator/
|
XML file that defines the property animation attributes. |
anim/
|
Defines the XML file of the tween animation. (The animation attributes can also be saved in this directory,animator/ The directory is used to save the animation attributes and separate the two directories. ) |
color/
|
Defines XML files for values of colors. For details, see color State List resource. |
drawable/
|
Bitmapfile (.png,.9.png,.jpg, .gif) or XML file that can be compiled as drawable: · Bitmap files · Nine-patches (re-Sizable bitmaps) State lists · Color drawables · Shapes · Animation drawables See drawable Resources |
layout/
|
For the XML file that defines the layout of the user interface, see Layout resource. |
menu/
|
Define the XML file of the program menus, such as options menu, context menu, or sub menu. See menu resource |
raw/
|
Any file must be in the original format. Use raw inputstream to open these resource files and call the resources. openrawresource () method to open resources corresponding to the resource ID, such as R. Raw. filename. However, if you need to access the original file name and file directory structure, you can consider saving some resource files in the assets/directory (instead of RES/raw /). Files placed in assets/do not provide a resource ID, so you can only use assetmanager to read them. |
values/
|
XML files containing some simple values, such as strings, integers, and colors. The XML resource file defined in other RES/subdirectories defines a separate Resource Based on the XML file name. The files in the values/directory describe various resources. For example, in a resource file under this directory, each <resources> sub-node defines a separate resource. For example, the <string> node creates an R. String resource, and the <color> node creates an R. Color Resource. Because each resource is defined by its own XML file, you can name the file and put different types of resources in the file. However, for clarity (for clarity), you may want to place a unified resource type in a file. For example, the following is a common convention for creating resource files in this directory: · Arrays. xml: Resource array (typed arrays) · Colors. xml: color values) · Dimens. xml: dimension values) · Strings. xml: string values) · Styles. xml: Placement style (Styles) See string resource, style resource, and more resource types. |
xml/
|
You can call the resources. getxml () method to read any XML file in the directory at runtime. All xml configuration files must be saved here, such as searchable Configuration |
ldpi
: Low-Density screens; approximately 120 DPI.
mdpi
: Medium-density (on traditional hvga) screens; approximately 160 DPI.
hdpi
: High-density screens; approximately 240 DPI.
xhdpi
: Extra high-density screens; approximately 320 DPI.Added in API Level 8
You can specify multiple qualifiers for a single set of resources, separated by dashes. For example,drawable-en-rUS-land
Applies to us-English devices in landscape orientation.
The qualifiers must be in the order listed in
Table 2. For example:
- Wrong:
drawable-hdpi-port/
- Correct:
drawable-port-hdpi/
Alternative resource directories cannot be nested. For example, you cannot haveres/drawable/drawable-en/
.
Values are case-insensitive. The resource compiler converts directory names to lower case before processing to avoid problems on case-insensitive file systems. Any capitalization in the names is only to benefit readability.
Only one value for each qualifier type is supported. For example, if you want to use the same drawable files for Spain and
France, youCannotHave a directory nameddrawable-rES-rFR/
. Instead you need two resource directories, suchdrawable-rES/
Anddrawable-rFR/
, Which contain the appropriate files. However, you are not required to actually duplicate the same files in both locations. Instead, you can create an alias to a resource. See
Creating alias resources below.
If there are two types of resources, put them in two different drawable directories and do not write them together.
If you do not want to repeatedly define the same file, you can associate the two types of resources. For example
<?xml version="1.0" encoding="utf-8"?><resources> <color name="yellow">#f00</color> <color name="highlight">@color/yellow</color></resources>
From: Docs \ guide \ topics \ resources \ providing-resources.html