Android Accessing Resource (Resource call)

Source: Internet
Author: User

After you provide some Resources in the application (discussed in Providing Resources), you can reference the resource ID to call related Resources. All resource ID numbers are defined in the R. class file of your project, which is automatically generated by the aapt tool.

 

When your application is compiled, the aapt tool will automatically generate the R. class file, which containsres/Directory of all resourcesID. Each resource type hasRCorresponding to (for example:R.drawableContains allDrawable ResourceAnd all resources of a specific type have a one-to-one static integer value (for example:R.drawable.icon). This integer value is the value of this specific resource.IDYou can use it to obtain your corresponding resources.

Although the R. class file defines the ID numbers of each type of resource, you do not need to go to the file to view the ID numbers of a resource. A resource ID generally consists of the following:

L Resource Type: each resource is grouped into a specific resource type, for examplestring,drawable, Andlayout. For more information about different Resource Types, see Resource Types.

L Resource name, which is also the file name, not including the extended name; or in XMLandroid:nameAttribute value,The condition is that the resource is a simple value (such as a string ).

There are two methods to call a resource:

LCall in code:UseR. class The static integer value in the corresponding subclass of, for example:

R. string. hello

stringYes. hello is the resource name. When you provide the resource ID in this way, many Android APIs can call your corresponding resources. For details, see: Call Resources in the code.

LInXML:The special XML syntax can also correspond to the relevant resource ID in your R. class file, for example:

@ String/hello

stringYes. hello is the resource name. You can call this syntax in XML wherever you need to use your own resources. For details, see: Call resources in XML.

 

 

 

Call RESOURCES IN CODE

You can call the resource ID as a method parameter in the code. For example, you can set an ImageView by callingsetImageResource()Method to useres/drawable/myimage.pngResource:

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

You can also callResourcesClassTo obtain a specific resource.getResources()Method.ResourcesClass.

Syntax

This is the syntax for referencing resources in code:

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

L<package_name>Package name of the resource (This field is not required when you want to apply the resource under your own package ).

L<resource_type>The class R corresponds to a subclass of a specific resource type)

L<resource_name>It can be a resource file name that does not contain the extension name of the file or an XML element.android:nameAttribute Value (only simple values, such as strings)

For more information about each Resource type and how to reference them, see Resource Types.

Use Cases

There are many ways to use the resource ID as a parameter. You can use the methods in Resources to obtain a corresponding resource. You can use the Context. getResources () method to obtain an instance of the Resources class ..

The following are examples of calling resources in code:

Note: A drawable resource is used to load the background of the current screen.getWindow().setBackgroundDrawableResource(R. drawable. my_background_image );

Note: You can set the title for the Activity by using the string obtained from the Resources object, because this method requires a collation column instead of a resource ID number.
getWindow().setTitle(GetResources ().getText(R. string. main_title ));

Note: load custom la s to the current screen
setContentView(R. layout. main_screen );

Note: You can use the animation obtained from the Resources instance to set an animation slide mFlipper.setInAnimation(AnimationUtils. loadAnimation (this,
R. anim. hyperspace_in ));

Note: use the resource ID to set the display text for TextView objects.
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);
 
Special reminder:You cannot manually modify the R. java file. This file is automatically generated by the aapt tool when your project is compiled. Modifications to all programs will be overwritten during the next compilation of the project.

InXMLCalling Resources

You can reference an existing resource to specify specific values for some XML attributes and elements. You often do this when creating layout files to provide strings and images for your parts.

For example, if you add a button to your layout, you should use a string resource to specify the text displayed on the button:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/submit" />
 
Note: @ string/submit is a string resource defined for you.

Syntax

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

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

L<package_name>Package name of the resource (This field is not required when you want to apply the resource under your own package)

L<resource_type>The class R corresponds to a subclass of a specific resource type)

L<resource_name>It can be a resource file name that does not contain the extension name of the file or an XML element.android:nameAttribute Value (only simple values, such as strings)

 

For more information about each Resource type and how to reference them, see Resource Types.

 

Use Cases

In some cases, you must use resources in XML (for example, apply a drawable image to a widget), but you can use resources in XML wherever simple values are acceptable. For example, if you have the following resource files that contain color resources and string resources:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <string name="hello">Hello!</string>
</resources>

You can use these defined resources to set the text color and content in the following layout files:

<?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="@color/opaque_red"
    android:text="@string/hello" />

In the following cases, you do not need to specify the package name When referencing resources because these resources are in your own current package. To reference a system resource, you must declare the package name of the resource when referencing it. For example:

<?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" />

Note:When you need to use strings, you should always use string resources, so your application can localize other languages. For information on creating Alternative Resources (such as localized strings), see Providing Alternative Resources.

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

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/other_drawable" />

This may sound redundant, but it is useful when you use alternative resources. Learn more about Creating alias resources.

 

Reference style attributes

A style property resource allows you to reference an attribute value in the topic of the current application. Referencing a style attribute allows you to customize the appearance of the UI element by styling the UI element to match the standard changes provided by the current topic, rather than providing a hard-coded value. Reference a style attribute. Basically, "Use a style defined by the attribute in the current topic ".

When a style attribute is referenced, the syntax of its name is almost the same as that of a common resource, but it replaces the symbol (@), which uses the question mark (?), The resource type is optional. For example:

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

For example, this example describes how to reference an attribute to set the color of the text to match the color of the "Main" text of the system subject:

<EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />

Here, the android: textColor attribute specifies the name of a style attribute in the current topic. Android now uses the value applied to the android: textColorSecondary style attribute as the android: textColor value in this part. Because the system resource tool knows that this attribute resource is expected by the Environment, you do not need to explicitly declare the type (which type may be? Android: attr/textColorSecondary)-You can exclude the attr type.

Call platform Resources

Android contains many standard resources, such as styles, themes, and layouts. To call these resources, you must use the android package name to limit your resources. For example, Android provides a layout resource, which can be used to list items in ListAdapter.

Note: ListAdapter refers to the list adapter.

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));

In this example, simple_list_item_1 is the layout resource defined by the platform for the ListView table items. You can use this list layout without creating a table item layout. (For more information about ListView, see List View Tutorial)

 

Welcome to reprint, reprint please indicate the source: http://www.cnblogs.com/CodeGuy/

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.