Notes on Android resource files

Source: Internet
Author: User

The Res/values folder of Android Studio provides developers with the ability to customize resource files. Refer to Google's documentation for a note.

The resource is placed under the subdirectory specified in the Res directory. The resources directory supported under the RES directory is as follows:

    animator/An XML file that is used to define property animations.

    anim/The XML file that defines the gradient animation. (Property animations can also be saved in this directory, but in order to differentiate between the two types, the property animation preferred animator/ directory.) )

    color/An XML file that defines a list of color states.

    drawable/A bitmap file (,,, .png .9.png ), .jpg .gif or a compilation of an XML file that draws an object resource subtype: A bitmap file, a nine-Gongge (resizable bitmap), a state list, a shape, an animation to draw an object, and other objects that can be drawn.

    mipmap/A drawing object file that is suitable for different launcher icon densities.

    layout/An XML file that defines the layout of the user interface.

    menu/An XML file that defines an application menu, such as a menu of options, a context menu, or a submenu.

    raw/Any file to be saved in its original form. To InputStream open these resources using the original, use the resource ID (that is R.raw.filename ) called Resources.openRawResource() .

However, if you need access to the original file name and file hierarchy, you might consider saving some resources in the assets/ directory instead res/raw/ . assets/the files in are not resource IDs, so you can only use AssetManager them to read them.

    values/An XML file that contains simple values such as strings, integers, and colors. An res/ XML resource file in another subdirectory defines a single resource based on the XML file name, and values/ the files in the directory can describe multiple resources. For files in this directory, <resources> each child element of the element defines a resource. For example, an <string> element creates R.string a resource, and an <color> element creates a R.color resource.

Because each resource is defined with its own XML element, you can name the file according to your needs and place different resource types in one file. However, for clarity, you may want to place unique resource types in separate files. For example, for a resource that can be created in this directory, the corresponding file name convention is given below: arrays.xml, used for resource arrays (typed arrays). Colors.xml: Color value. Dimens.xml: Dimension value. Strings.xml: String value. Styles.xml: Style.

Explain the values folder: Google means that the resource files under the values file are referenced according to the XML file name, but the resource files under the values folder are referenced by the child element type. You can create a new file of any name under values, define your own resources, and then simply refer to it through getresource (). getString (r.string.xxx) . But in order to uniform, Google has given us the name of these types of resources, but you can not, or build another file name, as long as the values under the <resources> tag in a file define the resource tags and values you want. For example, create a custom.xml file under the values folder, defined inside

                  <?xml version= "1.0" encoding= "Utf-8"?
<resources>
<bool name= "Boolean_value" >TRUE</BOOL>
<string name= "custom_string" > This                  is a custom string </string> under values;
<dimen name= "Custom_text_font" >18SP</DIMEN>
</resources> < Span style= "COLOR: #000080" > < Span style= "COLOR: #008000" > use.

< Span style= "COLOR: #000080" > < Span style= "COLOR: #008000" > < Span style= "COLOR: #000000" > do not save the resource file directly in the Res directory, the compilation will error. These are the default resources for Android. In addition, different layout resources and string text resources need to be prepared for devices with screen sizes that exceed the standard size or for systems with different language types. These are called alternate resources.

< Span style= "COLOR: #000080" > < Span style= "COLOR: #008000" > < Span style= "COLOR: #000000" > second, alternate resource

As shown, different types of devices or devices in different regions require multiple resources. Android detects the configuration of the device at run time and loads the appropriate resource files. Android to specify different resource files in a way:

    

    1. res/Create a <resources_name>-<config_qualifier> new directory in the form named.
      • <resources_name>is the directory name of the corresponding default resource (as defined in the resource directory).
      • <qualifier>is the name of each configuration that specifies the resources to use (see https://developer.android.com/guide/topics/resources/providing-resources.html).

      You can append more <qualifier> than one. Separate them with dashes.

      Note : When appending multiple qualifiers, you must place them in the same order listed in table 2 (see links above). If the qualifier is in the wrong order, the resource is ignored.

    2. Save the appropriate alternate resource under this new directory. These resource files must have exactly the same name as the default resource file.

For example:

res/

drawable/

Icon.png

Background.png

drawable-hdpi/

Icon.png

Background.png

When using the configuration qualifier name of an alternate resource, Android rules:

    • You can specify multiple qualifiers for a single group of resources and separate them with dashes. For example, for drawable-en-rUS-land horizontal U.S. English devices.
    • These qualifiers must follow the order listed in table 2 (red link above) . For example:
      • Error:drawable-hdpi-port/
      • That's right:drawable-port-hdpi/
    • You cannot nest alternate resource directories. For example, you cannot have a res/drawable/drawable-en/ .
    • The values are not case sensitive. Before processing, the resource compiler converts the directory name to lowercase to avoid problems with case-insensitive file systems. Any uppercase letters used in the name are for readability only.
    • Only one value is supported for each type of qualifier. For example, to use the same drawing object file for Spanish and French, you must not have a directory named, drawable-rES-rFR/ but you need two resource directories, such as and, that contain the appropriate drawable-rES/ files drawable-rFR/ . However, you do not actually have to copy the same files to both locations. Instead, you can create aliases that point to resources. is actually a multiple reference to a primitive resource.

When you save an alternate resource to a directory named after these qualifiers, Android automatically applies the resources to the app based on the current device configuration. Each time a resource is requested, Android checks to see if the alternate resource directory contains the requested resource file and then finds the best matching resource (described below). If you do not have an alternate resource that matches a specific device configuration, Android uses the appropriate default resource (a set of resources for a specific resource type that does not contain a configuration qualifier).

To enable your app to support multiple device configurations, it is important that you provide default resources for each resource type that your app uses, or it may crash on some locales or devices. See links for how Android finds the best match resource. As follows: The first step is to eliminate the resource files that contain qualifiers that conflict with the system settings. In the second step, select the qualifier with the highest precedence (next) in the list ( table 2). (Start with the MCC and Move Down.) Third step, is there a resource directory that includes this qualifier? If none, go back to step 2nd and look at the next qualifier. (In this example, the answer is always "no" unless the language qualifier is reached.) If so, please proceed to step 4th. The fourth step is to eliminate the resource directory that does not contain this qualifier. In this example, all directories without language qualifiers are eliminated. If the qualifier involved is screen pixel density, Android chooses the option closest to the device's screen density. Typically, Android tends to shrink large original images instead of zooming in on small original images. See support for multiple screens .

Fifth step, go back and repeat steps 2nd, 3rd and 4th until you have only one directory left. In this example, the screen orientation is the qualifier for the next judgment match. Therefore, resources that are not specified in the screen orientation are retired:

drawable-en/drawable-en-port/drawable-en-notouch-12key/

The rest of the catalogue is drawable-en-port .

Although this program is executed for each resource requested, some aspects of the system are still further optimized. For example, once the system knows the device configuration, it will retire an alternate resource that could never be matched. For example, if the configuration language is English ("en"), then any resource directory in which the language qualifier is set to non-English will never be included in the selected resource pool (however, the resource directory without the language qualifier will still be included in the pool).

When you select resources based on screen size qualifiers, if there are no better matching resources, the system will use resources designed for screens smaller than the current screen (for example, large screens will use standard-sized screen resources if necessary). However, if the only available resources are larger than the current screen, the system will not use these resources, and if no other resources match the device configuration, the app will crash (for example, if all layout resources are xlarge marked with qualifiers but the device is a standard-sized screen).

Note : The precedence Order of qualifiers (in table 2 ) is more important than the number of qualifiers that exactly match the device. For example, in the 4th step above, the last option remaining in the list includes three qualifiers (orientation, touch screen type, and input method) that match the device exactly, and drawable-en only one matching parameter (language). However, the precedence of the language is higher than the other two qualifiers and drawable-port-notouch-12key is therefore eliminated.

Notes on Android resource files

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.