Android Application Resources-Accessing Resources)

Source: Internet
Author: User

Once resources are provided in the application, resources can be used by referencing resource IDs. All resource IDs are defined in the project's R class, which is automatically generated by the aapt tool.

When an application is compiled, aapt generates the R class, which contains the resource IDs of all resources in the res/directory. For each type of resources, there will be an R subclass (for example, R. the drawable class can be used to draw all types of resources. For each type of resources, there will be a static INTEGER (for example, R. drawable. icon ). This integer is the resource ID that can be used to obtain resources.

Although the R class specifies the resource ID, you do not need to view it to find the resource ID. A resource ID is always composed of the following information:

Resource Type: each type of resource is grouped into one type, such as string, drawable, and layout.

Resource name: it can be a file name without the extension, or the value of the android: name attribute in the XML file (if the resource is a simple value like a string ).

There are two ways to access resources:

In the Code: Use a static integer from the R class subclass, such as R. string. hello. String is the resource type, and hello is the resource name. Some Android APIs can use this resource format to access resources.

In XML: You can also use the special XML syntax equivalent to the resource ID defined in the R class, such as @ string/hello.

String resource type. hello is the resource name. This syntax can be used in XML resources to place any value provided in the resource to the desired place.

Access resources in code

You can pass the resource ID as a parameter to the method to use the resource. For example, use the setImageResource () method to set the res/drawable/myimage.png resource to be used for an ImageView object:

ImageView imageView = (ImageView) findViewById (R. id. myimageview );
ImageView. setImageResource (R. drawable. myimage );

You can also use the getResources () method to obtain an example object of a resource.

Syntax

The syntax for referencing a resource in code is as follows:

[<Package_name>.] R. <resource_type>. <resource_name>

1. <package_name> is the name of the package where the resource to be located is located (the package name is not required when the referenced resource and code are in the same package ).

2. <resource_type> is a subclass of R, representing the resource type

3. <resource_name> it can be either a resource file name without an extension or an android: name attribute value in an XML Element (for a simple value resource)

Use Cases

There are many methods to receive resource IDS as parameters, and you can also use the methods in the Resources class to obtain Resources. Use the Context. getResources () method to obtain an instance of the Resources object.

The following are examples of resource access in the Code:

// Load a background for the current screen from a drawable resource
GetWindow (). 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 ID
GetWindow (). setTitle (getResources (). getText (R. string. main_title ));

// Load a custom layout for the current screen
SetContentView (R. layout. main_screen );

// Set a slide in animation by getting an Animation from the Resources object
MFlipper. setInAnimation (AnimationUtils. loadAnimation (this,
R. anim. hyperspace_in ));

// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById (R. id. msg );
MsgTextView. setText (R. string. hello_message );

Warning you do not need to manually edit the R. java file-it is generated by the aapt tool during project compilation. Any changes will be overwritten at next compilation.

Access resources in XML

When you customize values for some XML attributes, You can reference A existing resource value. This is often done when you create a layout resource, providing weidget strings and images.

For example, if a Button is added to the layout, a string resource should be used as the text of the Button:

<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/submit"/>

Syntax

The syntax for referencing a resource in an XML resource is as follows:

@ [<Package_name >:] <resource_type>/<resource_name>

<Package_name> indicates the name of the package where the resource to be searched is located (this package name is not required when the resource is referenced from the same package ).

<Resource_type> is a subclass of R, representing the type of the resource.

<Resource_name> it can be a resource file name without an extension, or an android: name attribute value in an XML Element (for a simple value resource)

Use Cases

In some cases, resources must be used in XML (for example, an image is used as an outline resource of a Widget), but resources in XML can be used wherever simple values can be received, for example, the following resource files contain a color resource and a string resource:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Colorname = "opaque_red"> # f00 </color>
<Stringname = "hello"> Hello! </String>
</Resources>

Use the preceding resource in the following layout file to set the text color and text string:

<? Xml version = "1.0" encoding = "UTF-8"?>
<EditTextxmlns: 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 = "@ string/hello"/>

In this case, you do not need to specify the package name because the resources in your package are referenced. To reference system resources, you must include the package name, for example:

<? Xml version = "1.0" encoding = "UTF-8"?>
<EditTextxmlns: 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"/>

Note: String resources should be used at any time so that applications can be localized for other languages.

You can even use resources in XML to create aliases. For example, you can create a drawable resource, which is the alias of another drawable Resource:

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

Reference component properties of a style

The Component Attribute of the referenced style resource allows you to reference a style in a topic as the property value. The Component Property Attribute of the referenced style matches the style topic with various standards supported by the current topic to achieve the effect of customizing the UI element appearance, rather than providing hard-coded values. The Component Attribute of the referenced style is actually used to use the style defined in the current topic.

The syntax of component attributes to reference a style is almost identical to that of common resources. The difference is that @ is? And the resource type is optional. For example:

? [<Package_name >:] [<resource_type>/] <resource_name>

For example, the following example shows how to set a text color attribute to match the "primary" text color of the system topic:

<EditTextid = "text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textColor = "? Android: textColorSecondary"
Android: text = "@ string/hello_world"/>

Android: textColor attribute specifies the name of a style attribute in the current topic. Android uses this value and uses the property value of the android: textColorSecondary style as the value of android: textColor In the widget. Because the system resource tool understands the expected resource attributes in this content, it does not need to specify a specific type (Which of the following is the complete reference in the above example? Android: attr/textColorSecondary --- the attr type can be ignored)

Access Platform Resources

Android contains many standard resources, such as styles, themes, and la S. To access these resources, you must use the full android package name to reference the resources. For example, Android provides layout resources for listing items in the ListAdapter class:

SetListAdapter (newArrayAdapter <String> (this, android. R. layout. simple_list_item_1, myarray ));

In this column, simple_list_item_1 is the layout resource defined by the platform for the project in the ListView object. You can use it to replace the layout of the list items you want to create.


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.