Data from official API documentation
creation and access of Android resource files
Android Adapter Create alias Resource
If you want to use a resource for multiple device configurations (but you do not want to provide it as a default resource), you do not need to put the same resource in multiple alternate resource directories. Instead, you can (in some cases) create an alternate resource that acts as an alias for a resource that is saved in the default resource directory.
Note: Not all resources provide the appropriate mechanism for you to create aliases that point to other resources. In particular, animation resources, menu resources, raw resources, and other unspecified resources in the xml/directory do not provide this functionality.
For example, join you have an app picture icon.jpg, and you need a unique version of a different locale. However, Canadian English and Canadian French both locales require the same version. You might think that you need to copy the same image to a resource directory in Canadian English and French, but that's not the case, instead, you can save the image as Icon_ca.png (any name except Icon.png) and put it in the default res/drawable/
directory. Then, res/drawable-en-rCA/
create the file in and, and use the res/drawable-fr-rCA/
icon.xml
< bitmap
> element to reference the icon_ca.png
resource. This way, you only need to store one version of the PNG file and two small XML files that point to that version. (XML file example below)
Drawable
To create a pointer to an existing Drawable
alias, use the < bitmap
> element. 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 save this file as icon.xml
(for example, in an alternate resource directory, Save as res/drawable-en-rCA/
), it is compiled into a R.drawable.icon
resource that can be referenced, but it is actually an R.drawable.icon_ca
alias for the resource (stored in res/drawable/
).
Layout
To create an alias to an existing layout, use the < > element that is wrapped in < merge
> include
. For example:
<?xml version="1.0" encoding="utf-8"?><merge> <include layout="@layout/main_ltr"/></merge>
If you save this file as main.xml
, it is compiled into a R.layout.main
resource that can be referenced, but it is actually an R.lyout.main_ltr
alias for the resource.
Strings and other simple values
To create an alias to an existing string, simply use the resource ID of the desired string as the value of the new string. For example:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello</string> <string name="hi">@string/hello</string></resources>
R.string.hi
The resource is now R.string.hello
an alias
Other simple values have the same principle. For example, color:
<?xml version="1.0" encoding="utf-8"?><resources> <color name="yellow">#f00</color> <color name="highlight">@color/red</color></resources>
leverage resources for optimal device compatibility
To provide the best device compatibility, always provide the default resources for the resources that are necessary for the app to run correctly. Then, use the configuration qualifier to create an alternate resource for a specific device configuration.
There is one exception to this rule: if you apply a minsdkversion of 4 or higher, you do not need a default drawable resource when you provide an alternate drawable resource with a screen density qualifier. Even without the default drawable resource, Android can find the best match from the alternate screen density and scale the bitmap as needed. However, in order to provide the best experience on all types of devices, you should provide alternate drawable for all three types of densities.
How Android finds the most matching resource
When you request a resource for which you want to provide an alternate resource, Android chooses the alternate resource to use at run time, based on the current device configuration. To demonstrate how Android chooses an alternate resource, assume that the following drawable directories contain different versions of the same image:
drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
Also, assume that the device is configured as follows:
Regional Settings = EN-GB
Screen orientation = port
Screen pixel density = hdpi
Touch screen type = Notouch
Main text IME = 12key
By comparing the device configuration with available alternate resources, Android drawable-en-port
chooses from the Drawable
.
The system uses the following logic to determine which resources to use:
1. Retire the resource files that conflict with the device configuration.
drawable-fr-rCA/
The directory conflicts with the EN-GB locale and is therefore eliminated.
drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
exception: screen pixel density is the only qualifier that has not been eliminated because of a conflict. Although the device has a screen density hdpi
, it is drawable-port-ldpi/
not eliminated because each screen density is considered a match at this time.
2. Select the highest-priority qualifier in the list (next). (Start with the MCC and Move Down.) )
3. 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.
4. Retire The resource directory that does not contain this qualifier. In this example, all directories without language qualifiers are eliminated.
exception: If the qualifier involved is screen pixel density, Android chooses the option closest to the screen density of the device. Typically, Android tends to shrink large original images instead of zooming in on small original images.
5. Go back and repeat steps 2nd, 3rd and 4th until only one of the directories is 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:
Access Resources
Code Access Resources
[< package_name
;] r.< resource_type
>.< recource_name
>
package_name
the name of the package that the < > resource is in (not required if the referenced resource is from your own resource bundle).
resource_type
R Subclass of < > Resource type.
< resource_name
> is a resource file name without an extension, or an attribute value in an XML element android:name
(if the resource is a simple value)
//Load a background for the current screens from a drawable resourceGetWindow (). Setbackgrounddrawableresource (R.drawable.my_background_image);//Set the Activity title by getting a string from the Resources object, because//This method requires a charsequence rather than a resource IDGetWindow (). Settitle (Getresources (). GetText (R.string.main_title));//Load a custom layout for the current screenSetcontentview (R.layout.main_screen);//Set a slide in animation by getting a animation from the Resources objectMflipper.setinanimation (Animationutils.loadanimation ( This, r.anim.hyperspace_in));//Set The text on a TextView object using a resource IDTextView Msgtextview = (TextView) Findviewbyid (r.id.msg); Msgtextview.settext (r.string.hello_message);
Accessing resources in XMLGrammar
[< package_name
;] r.< resource_type
>.< recource_name
>
- <package_name> is the name of the package where the resource is located (not required if the referenced resource is from the same package)
- <resource_type> is a class R subclass of resource type
- <resource_name> is a resource file name without an extension, or an Android:name property value in an XML element if the resource is a simple value.
<?xml version= "1.0" encoding= "Utf-8", <resources ; <color name = "opaque_red" ; #f00 </ color ; <string name = "hello" ; Hello!</string ; </resources ;
<!-- 要引用系统资源,您需要加入包名称 --><?xml version="1.0" encoding="utf-8"?><EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/secondary_text_dark" android:text="@string/hello" />
Referencing style properties
You can reference the value of an attribute in the style theme of the current app by using the Style property resource. By referencing style properties, you can customize the appearance of these elements by setting the style for the UI elements to match the standard variants provided by the current style theme, rather than providing hard-coded values for the UI elements. The essence of the reference style property is "Use the style defined by this property in the current style theme."
To reference style properties, the name syntax is almost identical to the normal resource format, except that the at sign (@) is changed to a question mark (?) and the Resource type section is optional. For example:
? [<package_name>:] [<resource_type>/]<resource_name>
<!--将文本颜色设置为与系统风格主题的“主要”文本颜色匹配--><EditText id="text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="?android:textColorSecondary" android:text="@string/hello_world" />
Accessing Platform Resources
Android contains many standard resources, such as styles, style themes, and layouts. To access these resources, qualify your resource reference with the Android package name. For example, you can use the layout resources provided by Android for list items in ListAdapter:
//simple_list_item_1 是平台为 ListView 中的项目定义的布局资源。您可以使用它,而不必自行创建列表项布局。setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));
Creation and access of Android resource files