Android: Dynamic acquisition of resource ID and resource ID

Source: Internet
Author: User

We usually obtain resources through the findviewbyid method. For example, we often use this statement in the oncreate method:

Btnchecked = (imageview) findviewbyid (R. Id.Imgcheck);

 

Findviewbyid is a convenient way to obtain various view objects in layout, such as buttons, labels, listview, and imageview. As the name suggests, it requires an int parameter: Resource ID.

Resource ID is very useful. Android automatically allocates an ID for each resource in the res directory, including the "@ + id" object in various image files and XML files. Res sub-directories are almost always fixed, such as drawable-XXXX, layout, values, and uncommon ones: anim, XML, row, and color.

Android textbooks tell you:

Res/drawable/is used to store image files;

Res/layout/is used to store layout definition files;

Res/values/is used to store variables, parameters, and other files.

We already know this. In addition, Android assigns IDs to all resources under the res directory. The main allocation principle is:

The image file in drawable is always a resource ID for each file.

In the XML file, each view that uses Android: Id = "@ + ID/XXX" is assigned an unused resource ID.

Other more complex rules may be added by everyone.

In the adk API, many methods actually use resource IDS as parameters, such as the getdrawable method:

Getresources (). getdrawable (R. drawable. sendsms_bk ));

Literally, the getresouces method returns an android. content. res. Resources object.

The getdrawalbe method returns a drawable object, which we know is an image.

The parameter used by the getdrawable method is the resource ID.

But what exactly are the numbers of these int type resource IDs? Or where they are stored. If you are careful enough, you can find them in the R. Java file of the gen directory. Each hex integer ID has a very O-O attribute name, they are all public static final, it is recommended that you do not manually modify them. These resource IDs are located in different class hierarchies. If you take a closer look, you will find some rules. For example, the drawable class contains resource IDs, which actually represent the image files in the Res/drawable directory; the resource ID contained in the layout class actually represents the XML file in the Res/layout directory; the resource ID contained in the ID class actually represents the layout XML file using Android: id = "@ + ID/XXX" widget object -- of course, if you are using "@ + tyre/XXX", Android will be in the R. create a class named tyre in Java.

Through the above understanding, we found that the resource ID is "R. "variables or attributes starting with" -- because they are all defined in the r class -- this class is similar to Android. R is not the same R and they are not in the same package level. The former defines the resources of the android framework, such as Android. R. layout. simple_expandable_list_item_1 and Android. R. layout. simple_expandable_list_item_2. Have you used these resources? Try it if you have time. Another R is our own R, which defines our own resource ID. We can use the resource ID in the object-oriented R. XXX. YYY method. Of course, we can also use them directly in hexadecimal notation.

Resource ID is so important in Android that we only need to obtain the resource ID. In some cases, we cannot use a constant as the resource ID (because we want to replace it with a variable), so we can only dynamically obtain the resource ID in two ways:

1. Reflection

Java reflection is so powerful that we always think of it when there is no way to go. Let's look at a piece of code:

Try{

Field field = R. drawable.Class. Getfield (type );

IntI = field. getint (NewR. drawable ());

Log.D(Tag, I + "");

ReturnI;

}Catch(Exception e ){

Log.E(Tag, E. tostring ());

ReturnR. drawable.Unknow;

}

Type is a variable. Its possible values include "close", "edit", and "icon. We access a field under the R. drawable class through the reflection field of Java. In fact, we know that the field in R. drawable is the resource ID of the image file under the res. drawable-XXXX directory. Therefore, the above code is used to obtain the resource ID of the corresponding image file through a string. For example, if the value of type is "icon", the above Code will obtain the resource ID of the icon.png image file under the res. drawable-XXXX directory and return it to us. It is logical to display images in imageview by resource ID.

2. Use the resources class getidentifier Method

Reflection is troublesome after all. If the same thing is usedAndroid. content. res. ResourcesClass, just two sentences:

Resources res = getresources ();

Return res. getidentifier (type, "drawable", getpackagename ());

The getresources method is from contenxt (that is, the activity class), which can directly return a resouces object. The getidentifier method of resouces can return any resource ID in R. java. Of course, you must specify three parameters: field name, class name, and package name. The fully qualified package name specified by the package name. If the fully qualified package name of R is Android. R or com. company. r, the package name is "android" or "com. company ". Getpackagename is actually this. getpackagename (), which directly returns the package name of this class.

The class name is the class to which the resource belongs. As we know, there are several fixed classes in the R. Java class: drawable, ID, string, and layout, and many resource IDs are defined below them.

The field name is the name of the resource ID. For example, the resource ID is defined as follows:Public static final int Del= 0x7f020002;

Del is the name of a resource ID. 0x7f020002 is its hexadecimal value.

With three parameters, the getidentifier method can obtain the resource ID dynamically.

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.