Resources are additional files that you use in your code and are packaged into your application during compilation. Android supports a variety of different files, including XML, PNG, and JPEG files. The format of an XML file depends on the content it describes. These files describe the types, syntaxes, or formats supported by the files.
In consideration of loading efficiency, resources are separated from the code, and XML files are compiled into binary code. String, which is compressed and stored in a more economical format. For one reason or another, many different resource and resource definition files exist in the Android platform.
These files include the following parts:
* Resources
O create resources
O resource usage
§ Use resources in code
§ Reference resources
§ Reference subject attributes
§ Use system attributes
O change resources
O resource reference
O Glossary
* Internationalization (l18n)
In combination with resource reference documents, Android provides an economical resource processing technology. It converts a large amount of resource information so that android does not need to understand the document content during runtime. It is enough to instruct Android wherever you need it.
Resources
This section contains a resource-related Glossary and a series of resource usage code examples. For more information about Android resources, see "resources.
The android resource system saves all the stubs of resources unrelated to the code. You can use the resources class to access the resources of your application. The resource instances associated with the application can be obtained through context. getresources.
The resources of an application can be compiled into binary applications by the build system during construction. To use resources, you must correctly initialize the Resource Tree in your application and build your application. In the building process, the identifiers of each resource are generated and you can use them in the code. These identifiers allow the compiler to verify that the code and resource definitions in your application match.
For other details of this chapter, see the section on how to use resources in the tutorial.
Create Resources
Android supports strings, bitmaps, and many other types of resources. The syntax and format of each resource definition file and the storage location depend on the object on which it depends. Generally, you can create resources through three types of files: XML files (except bitmap and native files), bitmap files (as images), and Native files (all other types, such as audio files ). In fact, there are two different types of XML files, one is compiled as a resource near the application, and the other is the description of the resource, which is used by aapt. The following table details the types and structures of these XML files.
You can create and store resource files in the appropriate sub-directories under the Res/directory of your project. Android uses the resource compiler to access sub-directories and formatted files of resources. The following table lists the file types of each resource. For more information about objects, syntaxes, and formats, see resource reference.
Directory |
Resource Type |
Res/anim |
The XML file is compiled into a sequence animation or an automatic animation object. |
Res/drawable |
. PNG, 9.png,. jpg files are compiled as drawable resource subtypes: Use resources. getdrawable (ID) to obtain the resource type. * Bitmap files * 9-patchs (variable bitmap file) |
Res/Layout |
Compile the resource as a screen la er. See the "la Er" Chapter |
Res/Values |
XML files can be compiled into multiple resources. Note: unlike other RES directories, this directory can contain multiple Resource Description files. The element type of the XML file controls where these resources are stored by the R class. You can customize the names of these files. There are some conventional files. * Arrays. xml defines an array. * Colors. xml defines the colors of the printable objects and strings. You can use resources. getdrawable () and resources. getcolor () to obtain these resources. * Dimens. xml defines the scale. Use resources. getdimension () to obtain these resources. * Strings. xml defines strings (using resources. getstring () or the more appropriate resources. gettext () method to obtain these resources. The resources. gettext () method retains all descriptors used to describe the user interface style, keeping the original appearance of complex text. * Styles. xml defines the style object |
Res/XML |
Custom XML file. These files will be compiled near the application at runtime, and can be obtained at runtime using the resources. getxml () method. |
Res/raw |
Custom Native Resources are directly copied to the device. These files will not be compressed close to your application. Use the resources. getrawresource () method with the ID parameter to obtain these resources, such as R. Raw. somefilename. |
The resource is finally compiled into the APK file. Android creates a package class R, and you can use it to retrieve resources. R contains subclasses with the same name as the Resource Directory.
Global Resources
* Many resources allow you to define colors. Android accepts web-style color definitions-a group of hexadecimal color values, arranged in the following sequence: # RGB, # argb, # rrggbb, # 20.rggbb.
* All colors support alpha channels, and the first two hexadecimal numbers specify transparency. 0 indicates full transparency in the alpha channel. The default value is opacity.
Use resources
This section describes how to use the resources you have created, including:
· How to use resources in code.
· How to reference resources in other resources you can reference resources from other resources. You can reuse the internal general resources of the resource.
· Supports resource replacement. You can specify different resources based on the language, display configuration, and host hardware during loading.
During compilation, Android generates a class named R. R contains all the classes used by your application. This class contains sub-classes with the same name as the sub-directories under Res. These subclasses contain the identifier of each resource that you define in the resource file. These resource IDS can be referenced in your code. Here is a simple resource file that contains some text, la er, and image resources.
Note: The R class is automatically generated and cannot be manually modified. It is automatically modified when the resource changes.
Package com. Android. samples;
Public final class r {
Public static final class string {
Public static final int greeting = 0x0204000e;
Public static final int start_button_text = 0x02040001;
Public static final int submit_button_text = 0x02040008;
Public static final int main_screen_title = 0x0204000a;
};
Public static final class layout {
Public static final int start_screen = 0x02070000;
Public static final int new_user_pane = 0x02070001;
Public static final int select_user_list = 0x02070002;
};
Public static final class drawable {
Public static final int company_logo = 0x02020005;
Public static final int smiling_cat = 0x02020006;
Public static final int yellow_fade_background = 0x02020007;
Public static final int stretch_button_1 = 0x02020008;
};
};
Use resources in code
To use resources in code, you need to know the complete resource ID and the type of your resource object. The resource reference syntax is as follows:
R. resource_type.resource_name or android. R. resource_type.resource_name
Resource_type is a subclass of the r class that saves the specified type resources. Resource_name is the resource name defined in the XML file or the resource file name (without an extension) defined by other file types. Each type of resource is based on its type, added to a specified R subclass. To learn the type Relationship of R subclass, refer to the resource reference section. Your application can reference compiled resources without the package name (for example, R. resource_type.resource_name ). Android contains the serial number of a standard resource, such as the screen style and button background. To reference these resources, you must use the syntax with Android, such as Android. R. drawable. button_background.
Below are some examples of good and bad code:
// Load a background for the current screen from a drawable resource.
This. getwindow (). setbackgrounddrawableresource (R. drawable. my_background_image );
// Wrong sending a string resource reference into
// Method that expects a string.
This. getwindow (). settitle (R. String. main_title );
// Right need to get the title from the resources wrapper.
This. getwindow (). settitle (resources. gettext (R. String. main_title ));
// Load a custom layout for the current screen.
Setcontentview (R. layout. main_screen );
// Set a slide in animation for a viewflipper object.
Mflipper. setinanimation (animationutils. loadanimation (this,
R. anim. hyperspace_in ));
// Set the text on a textview object.
Textview msgtextview = (textview) findviewbyid (R. Id. msg );
Msgtextview. settext (R. String. hello_message );
Reference resources
A property value (or resource) can also reference resources. This usage is often used in text and images (defined in other files) in resource la er files ). This method can reference any resource, including the color and integer.
For example, if we have a color resource, we can write a layout file that specifies the text color and size.
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: textcolor = "@ color/opaque_red"
Android: text = "Hello, world! "/>
Note: the "@" prefix declares that this is a resource reference-the subsequent text is the resource name provided in the form of @ [Package:] type/name. In this example, we do not need to specify a specific package, because we reference it in our own package. When referencing a system file, we need to write as follows:
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: textcolor = "@ Android: color/opaque_red"
Android: text = "Hello, world! "/>
In other examples, you will often see resource references for localization.
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: textcolor = "@ Android: color/opaque_red"
Android: text = "@ string/hello_world"/>
This simple method can be referenced between resources. For example, we can create a new printable object, an alias for a real image:
@ Android: drawable/theme2_background
Reference topic attributes
Other types of resources can reference the attribute values of the current topic. These attributes can only be style resources or XML attributes. In this way, you can customize your UI to maintain a consistent style with the current topic.
The following example shows how to use the system topic to set the text color in the la Er:
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: textcolor = "? Android: textdisabledcolor"
Android: text = "@ string/hello_world"/>
Note: The method for referencing a topic attribute is similar to that for referencing a resource. The method is simply "?". Replace "@". When you use this tag, the resource name you provide must be located in the topic attribute, because the resource tool considers this resource attribute to be expected, you do not need to specify the expenditure type (? Android: ATTR/Android: textdisabledcolor ).
Use System Resources
Many resources contained in the system can be accessed by applications. All resources are defined in the Android. r class. For example, you can use the code to display the icon of the standard application on the screen:
Public class myactivity extends Activity
{
Public void onstart ()
{
Requestscreenfeatures (feature_badge_image );
Super. onstart ();
Setbadgeresource (Android. R. drawable. sym_def_app_icon );
}
}
Similarly, the following code changes the subject of your system:
Public class myactivity extends Activity
{
Public void onstart ()
{
Super. onstart ();
Settheme (Android. R. style. theme_black );
}
}
Provide Replaceable Resources for languages and configurations
You can select different UI languages and hardware configurations for your applications. Note that, although you can use different strings, layout manager, and many other resources, the SDK does not expose the method that allows you to display the specified resources to be loaded. Android recognizes the hardware and localization attributes of the entire set of resources, and then loads them as appropriate. You can change the language settings in the settings Panel of the device.
To include Replaceable Resources, create parallel resource directories and name these directories in a limited manner. The configuration files also apply (languages, screens, etc ). The following example shows how to support both English and French:
MyApp/
Res/
Values-en/
Strings. xml
Values-fr/
Strings. xml
Android supports multiple naming restrictions and diverse value styles. Add the information to the end of the Resource Directory and separate it with the connection number. You can add multi-segment limits for a single directory. However, they need to maintain a certain order for easy reading and understanding. The following example shows the full name of a qualified resource directory of an object:
MyApp/
Res/
Drawable-en-rUS-port-160dpi-finger-keysexposed-qwerty-dpad-480x320/
More representative, you may need to specify some resource configuration parameters. You may need to remove some values from the complete list, but many other values are retained in the list:
MyApp/
Res/
Drawable-en-Rus-finger/
Drawable-Port/
Drawable-port-160dpi/
Drawable-QWERTY/
Qualifier |
Value |
Language |
Two-byte ISO 639-1 low-level language encoding, such as en, FR, es |
Region |
Two bytes of ISO 3166-1-alpha-2 are encoded in uppercase with the lowercase "R" prefix. Example: Rus, RFR, res |
Screen direction |
Port, land, square |
Screen pixel density |
92 DPI, 108 DPI, etc. |
Touch screen type |
Notouch, stylus, finger |
Can a user use a keyboard? |
Keysexposed, keyshidden |
Default text input method |
Nokeys, Qwerty, 12key |
Default non-touch Navigation Method |
Notouch, DPAD, trackball, and wheel |
Screen Size |
320x240,640x480, etc. large dimensions must be declared first. |
The sub-List does not include custom device parameters, such as signals and trademarks. The information that all applications should know must be encoded according to the list information above.
Here are some general naming principles:
· Connect values with Connection Numbers
· The value is case sensitive, for example
O A specified drawable directory must be named drawable-port instead of drawable-port.
· Each qualified word can only be selected. You cannot name the directory drawable-ren-RFR /.
· You can add multiple qualified words, but each qualified word must be in the list above. For example, drawable-en-Rus-land indicates that it is used for the American English design of the dashboard.
· Directories with qualified words cannot be nested. For example, Res/drawable-en is not allowed.
· Resource references are represented as unmodified expressions in syntax, such as MyApp/RES/drawable-port-92dp/myimage.png, represented as: @ drawable/myimage in XML, represented: r. drawable. myimage.
How does Android find the most matched directory?
Android selects resources based on the configuration file. The selection process is as follows:
1. Exclude all resources that do not match the current device parameter specified in the configuration file. For example, if the screen pixel density is 108 DPI, only MyApp/RES/drawable-port-92dpi/will be excluded from the directories listed below.
MyApp/RES/drawable/myimage.png
MyApp/RES/drawable-en/myimage.png
MyApp/RES/drawable-Port/myimage.png
MyApp/RES/drawable-port-92dpi/myimage.png
2. Then, the system will filter the number of qualified words. For example, if your local system identifies it as an en-GB-encoded portable device, then we have two remaining directories to match.
MyApp/RES/drawable/myimage.png
MyApp/RES/drawable-en/myimage.png
MyApp/RES/drawable-Port/myimage.png
3. The selection mechanism defines the priority in the list order above based on the priority defined in the configuration file.
MyApp/RES/drawable-en/myimage.png
MyApp/RES/drawable-Port/myimage.png