Android Application Resources-provide Resources (2)

Source: Internet
Author: User

Provide alternative resources

Almost every application should provide alternative resources to support special device configurations. For example, you should include optional trace resources for different screen resolutions and provide optional string resources for different language environments. At runtime, the Android system detects the current device configuration and loads appropriate resources for the application.

To specify a set of optional resources for a special configuration, follow these steps:

1. use the <resources_name>-<config_qualifier> Format to create a new directory in the res/directory. <resources_name> is the Directory Name of the corresponding default Resource (defined in table 1 ); <qualifier> is a separate configuration name (as defined in Table 2) specified for devices that use these resources ). Each name can be added with a short line <qualifier>

Warning when adding multiple delimiters, they must be placed in the order listed in table 2. If the delimiter order is incorrect, the resource is ignored.

2. Save the optional alternative resources in the new directory. These resource files must be named exactly like the default resource files.

For example, the following are definitions of default and optional resources:

Res/

Drawable/

Icon.png

Background.png

Drawable-hdpi/

Icon.png

Background.png

The hdpi qualifier indicates that resources in this directory are used for high-resolution screens. Images in each descriptive directory are prepared for screens with specific resolutions, but their file names are identical. This example uses the same resource ID as the icon.pngand background.png images. The Android system compares the device configuration information with the delimiter in the Resource Directory Name to select the best resource version that matches the current device.

Android supports several configuration delimiters, and multiple delimiters can be added in a directory name separated by a short line. Table 2 lists the valid configuration delimiters in priority order. To use multiple delimiters in the Resource Directory, add them to the directory names in the order listed in table 2.


Table 2. Configure the qualifier name
 

Configuration Qualifier Value Description
MCC and MNC For example:
Mcc310
Mcc310-mnc004
Mcc208-mnc00
And so on
MCC is The mobile country code, followed by a mobile network code (MNC: mobile network code) that selectively follows The SIM card from The device ). For example, in any carrier, mcc310 stands for the United States, mcc310-mnc004 stands for the United States's Venizon company, mcc208-mnc00 stands for the French Orange Company.
If the device uses an audio connection (GSM phone), the value of MCC and MNC comes from the SIM card.
MCC can also be used independently (for example, including legal resources in special countries in applications ). If you only need to specify the language environment, you can use the language and region qualifier instead (discussed later ). If you decide to use the MCC and MNC delimiters, You need to carefully test them so that they can meet your expectations.
You can also view the configuration domain mcc and mnc, which indicate the current mobile country code and mobile network Code respectively.
Mcc: http://developer.android.com/reference/android/content/res/Configuration.html#mcc
Mnc: http://developer.android.com/reference/android/content/res/Configuration.html#mnc
 
Language and region For example:
En
Fr
En-rUS
Fr-rFR
Fr-rCA
The language is defined with two letters of the ISO 639-1 language code, followed by two optional ISO-3166-1-appha-2 area code letters (front is lowercase "r ").
This encoding is case-insensitive. The r prefix is used to distinguish the region and cannot be specified separately.
If the user changes the language settings in the system, the corresponding language can also be changed during the running of the application.
Minimum Width Sw <N> dp
For example:
Sw320dp
Sw600dp
Sw720dp
And so on
The basic screen size refers to the shortest available area. Specifically, the minimum width of a device is the shortest width and height of the screen (or the minimum possible width of the screen ). In this way, you can use this qualifier to ensure that the application has at least <N> dp width for the UI interface, regardless of the current direction of the screen.
For example, if the layout requires a minimum screen size of at least 600dp at any time, you can use this qualifier to create layout Resources in the res/layout-sw600dp/directory. The system uses these resources only when the available screen size is at least 600dp, regardless of whether the 600dp is the height or width recognized by the user. The minimum width is the fixed screen size feature of the device. When the screen direction changes, the minimum width of the device does not change.
The minimum width of the device must consider the screen decoration and the occupation of the system UI. For example, if a device has some Fixed UI elements going along the axis of the smallest width and occupying a certain amount of screen space, the minimum width declared by the system is smaller than the actual screen size, because the pixels occupied by the system are invalid for the UI of the user application. Therefore, this value should be the minimum actual size required for the application layout (normally, this value is the minimum width supported by the layout regardless of the current direction of the screen ).
The following are some values of common screen sizes that can be used:
1.320. devices configured for the following screens:
240x320ldpi (QVGA handheld device)
320x480 mdpi (handheld device)
480x800 hdpi (High Resolution handheld device)
2.480, for X mdpi screen (tablet or handheld device)
3.600, for 600x1024mdip screens (7 inch fl)
4.720, for 10 inch x1280mdip screen (tablet)
When an application provides multiple resource directories with different minimum width qualifiers, the system uses the resource that is closest to (not exceeding) the minimum width of the device.
This qualifier is added to API Level 13.
For more information, see the android: requiresSmallestWidthDp attribute. It declares the minimum and minimum widths compatible with your application, and the smallestScreenWidthDp configuration field holds the value of the minimum width of the device.
Available width W <N> dp
For example:
W720dp
W1024dp
And so on
Specify the minimum available screen width. The value of <N> should be defined in the unit of dp in the resource. When the direction changes between the horizontal and vertical directions, this configuration value will match the current actual width.
When the application provides multiple resource directories with different values for this configuration, the system uses the configuration closest to (not exceeding) The current screen width of the device. This value must take into account the space occupied by the screen decoration. Therefore, if the device has some Fixed UI elements on the left or right side of the display, the width is smaller than the actual screen size, because of the occupation of these Fixed UI elements, the available space of the application is reduced.
This feature is added to API Level 13.
The screenWidthDp configuration field holds the current screen width.
Available height H <N> dp
For example:
201720dp
H1024dp
And so on
Specify the minimum available screen height. The value of <N> should be defined in the unit of dp in the resource. When the direction changes either horizontally or vertically, this configuration value should match the current actual height.
When the application provides multiple resource directories with different values for this configuration, the system uses the configuration closest to (not exceeding) The current screen height of the device. Therefore, if a device has Fixed UI elements at the top or bottom of the display, the height is smaller than the actual screen size, because of the occupation of these Fixed UI elements, the available space of the application is reduced. Unfixed screen Decoration (for example, the phone status bar can be hidden in full screen) is not considered, and window decoration such as the title bar or operation bar is not considered, therefore, the application must be prepared to process less space than they specify.
This qualifier is added to API Level 13.
The screenHeightDp configuration field holds the height of the current screen.
Screen Size Small
Normal
Large
Xlarge
Small: this screen is similar to a low-resolution QVGA screen. The minimum layout size for a small screen is approximately 320x0000dp. For example, QVGA low resolution and VGA high resolution.
Normal: this screen is similar to a medium-resolution HVGA screen. The minimum layout size for a normal screen is approximately 320x470dp. For example, WQVGA low-resolution screen, HVGA medium-resolution screen, and WVGA high-resolution screen.
Large: this screen is similar to a medium-resolution VGA screen. The minimum layout size for a large screen is approximately x DP. For example, VGA and WVGA moderate-resolution screens.
Xlarge: this screen is considered to be larger than the traditional medium-resolution HVGA screen. The minimum layout size for the xlarge screen is approximately x960dp. In most cases, this ultra-large screen device needs to be carried in a backpack because it is too large, and most likely is a tablet-style device.
Note: using the size qualifier does not mean that the resource is only used for the screen of this size. If no qualifier is provided to provide the optional resources that match the current device configuration, the system uses the resource closest to the configuration.
Warning if all the resources using the dimension qualifier are larger than the current screen, the system will not use them and the application will crash at runtime (for example, if all la s are marked with xlarge delimiters, the device is a normal-sized screen ).
This qualifier is added to Versions later than API Level 4.
Screen appearance Long
Notlong
Long: long screen, such as WQVGA, WVGA, FWVGA
Notlong: Non-long screen, such as QVGA, HVGA, VGA
This qualifier is added to Versions later than API Level 4.
This qualifier is completely based on the screen appearance ratio, not relative to the screen direction.
Check the screenLayout configuration field, which indicates whether the screen is a long screen.
Screen direction Port
Land
Port: vertical device (vertical)
Land: horizontal device (horizontal)
If the user rotates the screen, this limit can be changed during the application program running.
The orientation configuration field indicates the direction of the current device.
Berth Mode Car
Desk
Car: the device stops in the car.
Desk: the device stops at the desk.
This qualifier is added to Versions later than API Level 8.
If the user changes the location of the device, this limitation can be changed during the application operation. You can use the UiModeManager object to enable or disable this mode.
Night mode Night
Notnight
Night: night
Notnight: daytime
Added to Versions later than API Level 8
If the night mode is retained in the automatic mode (default), the mode is changed based on the daytime time during the application running. You can use the UiModeManager object to enable or disable this mode.
Screen pixel density (dpi) Ldpi
Mdpi
Hdpi
Xhdpi
Nodpi
Tvdpi
Ldpi: low-resolution screen with around DPI;
Mdpi: medium-resolution screens (on traditional HVGA) with around DPI );
Hdpi: high-resolution screen with around DPI;
Xhdpi: for ultra-high resolution screens of approximately DPI, it is added to Versions later than API base 8;
Nodpi: this limit is used for bitmap resources that do not want to scale based on the resolution of the matching device.
Tvdpi: the screen between the mdpi and hdpi, which is about 213 dpi. This type of grouping is not the main resolution, most of which is for TV, and most applications do not need it-providing mdpi and hdpi resources can meet the needs of most applications, and the system scales them properly. This qualifier is introduced after API Level 13.
The scaling ratio between the four major resolutions is: 3: 4: 6: 8 (ignore the tvdpi resolution), so a 9x9 ldpi bitmap, in mdpi, It is 12x12, 18x18 in hdpi, and 24x24 in xhdpi.
If you feel that the image resources on TV or some other devices are not good-looking and you want to try tvdpi resources, the scaling factor is 1.33 * mdpi. For example, an mdpi image of 100px x 100px should be scaled into a tvdpi image of 133px x 133px.
Note: using a resolution qualifier does not mean that the resource is only applicable to the screen with the corresponding resolution. If no available resource matches the current device configuration, the system uses the closest resource.
Touch screen type Notouch
Stylus
Finger
Notouch: Non-touch screen device
Stylus: resistance screen devices for pen
Finger: Touch Screen Device
The touchscreen configuration field indicates the touch screen type on the device.
Keyboard availability Keysexposed
Keyshidden
Keyssoft
Keysexposed: the device has an available keyboard. If the keyboard is enabled, you can use this qualifier even if the keyboard is not exposed to users. If no keypad is provided or disabled, you can use this qualifier only when the keypad is exposed to users.
Keyshidden: the device has an available hard keyboard, but it is hidden and there is no available soft keyboard.
Keyssoft: the device has an available keyboard, whether visible or not.
If you provide keysexposed resources but do not have keyssoft resources, the system uses keysexposed resources no matter whether the keyboard is visible as long as the system has an available soft keyboard.
If you enable the hard keyboard, you can change this limitation during the application running.
The hardKeyboardHidden and keyboardHidden configuration fields indicate the visibility of the hard keyboard and the visible keyboard type (including the soft keyboard ).
Main text input method Nokeys
Qwerty
L2key
Nokeys: The device does not have a hard keyboard for text input;
Qwerty: the device has a standard hard keyboard, whether visible or not;
12 key: the device has 12 keys, regardless of whether the user is visible.
The keyboard configuration field specifies the available main text input methods.
Validity of the navigation key Navexposed
Navhidden
Navexposed: the navigation key is available to users;
Navhidden: the navigation key is unavailable.
If you can see the navigation key, you can change this restriction when the application is running.
The navigationHidden configuration field indicates whether the navigation key is hidden.
Main non-touch screen navigation methods Nonav
Dpad
Trackball
Wheel
Nonav: the device has no navigation facilities except touch screens.
Dpad: the device has a d-pad for navigation ).
Trackball: the device has a trackball for navigation.
Wheel: the device has a navigation wheel (uncommon ).
The navigation configuration field specifies the available navigation method type.
Platform version (API level) For example:
V3
V4
V7
And so on
API level supported by the device. For example, v1 indicates API Level 1 (devices with Android1.0 or higher) and v4 indicates API level 4 (devices with Android1.6 or higher)
Warning Android1.5 and 1.6 can match resources only when the qualifier exactly matches the platform version.
Note: Some configuration delimiters have been added since Android1.0, so not all Android versions support all delimiters. When a new qualifier is used, the platform version qualifier is implicitly added so that the old device can ignore the qualifier. If the w600dp qualifier is used, the v13 qualifier is automatically included. This is because the Valid Width qualifier is supported after API Level 13. To avoid some problems, you must always include a set of default resources (this group of resources has no qualifier ).
 

Qualifier naming rules

The naming rules for using the configuration qualifier are as follows:

1. Multiple delimiters can be specified for a single resource set. The delimiters are separated by hyphens. For example, drawable-en-rUS-land specifies a horizontal American English device.

2. the qualifier must be used in the order listed in table 2, for example:

Incorrect usage: drawable-hdpi-port

Correct usage: drawable-port-hdpi

3. Optional resource directories cannot be nested. For example, the following directory structure cannot be used:

Res/drawable-en/

4. The value is case sensitive. To avoid case-sensitive file system problems, the resource compiler converts the directory name to lowercase. All uppercase letters in the name are for ease of reading.

5. Each qualifier type supports only one resource value. For example, if you want to use the same descriptive file for Spain and France, you cannot name the directory name drawable-rES-rFR /. On the contrary, two directories are required, such as drawable-rES and drawable-rFR/. Each directory contains the corresponding files. However, you do not need to copy the same file in these two directories. You can use the resource name alias to solve this problem. For details, see "create resource alias" below ".

After these optional resources are saved to the qualified naming directories, Android automatically applies the corresponding resources in the application based on the current device configuration. Every time a resource is requested, Android checks the optional resource directory containing the requested resource file and finds the most matched resource. If no available resource matches the actual device configuration, Android uses the corresponding default Resource (a group of resources that do not contain the configuration qualifier ).

Create an alias for a resource

When you want to configure multiple devices to use the same resource (but do not plan to use this resource as the default resource), you do not need to put the same resource in multiple optional resource directories. On the contrary, in some cases, you can create an alias for an optional resource just as you saved it in the default Resource Directory.

Note: not all resources provide a mechanism to create an alias pointing to another resource. In fact, non-specific resources in the animation/, menu/, raw/, and xml/directories do not provide this function ..

For example, suppose there is an application icon: icon.png, which requires a unique version in different regions. However, there are two regions, Canada English and Canada French. They need to use the same version. It can be assumed that the same image needs to be copied to the resource directories of the English region of Canada and the French region of Canada, but this is not actually required. You can save this image (any name other than icon.png) with the icon_ca.png name and put it in the default res/drawabel/directory, create a cion In the res/drawable-en-rCA/and res/drawable-fr-rCA/directories. xml file, use the <bitmap> element to direct to the icon_ca.png resource. In this way, only one version of PNG file can be saved, and two small XML files point to it.

Resource alias can be traced

Use the <bitmap> element to create an alias for an existing trace resource, for example:

? Xml version = "1.0" encoding = "UTF-8"?>
<Bitmap xmlns: android = "http://schemas.android.com/apk/res/android"
Android: src = "@ drawable/icon_ca"/>

If you use this file as an icon. xml to save it in an optional Resource Directory, such as res/drawable-en-rCA/, then it will be compiled into R. drawable. icon to reference the resource, but it is actually R. drawable. an alias of the cion_ca resource.

Layout resource alias

Use the <include> element encapsulated in the <merge> element to create an alias for an existing layout, for example:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Merge>
<Include layout = "@ layout/main_ltr"/>
</Merge>

If you save this file as main. xml, then it will be compiled into R. layout. main to reference a resource, but in fact it is R. layout. the alias of the main_ltr resource.

Resource alias of string and other simple values

Simply use the resource ID of the expected string as the value of the new string, you can create an alias for an existing string, for example:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello </string>
<String name = "hi"> @ string/hello </string>
</Resources>

The R. string. hi resource is now an alias for the R. string. hello resource.

Other simple value resources also use the same method, such as a color value:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color name = "yellow"> # f00 </color>
<Color name = "highlight"> @ color/red </color>
</Resources>



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.