We usually obtain resources through the Findviewbyid method, for example, we often use such statements in the OnCreate method:
Btnchecked= (ImageView) Findviewbyid (r.id. Imgcheck);
Findviewbyid is a convenient way for us to get the various view objects in layout such as buttons, tags, listview, and ImageView. As the name implies, it requires an int parameter: Resource ID.
Resource IDs are useful. Android back automatically assigns an ID to each resource located in the Res directory, including various picture files, the "@+id" object in the XML text. The sub-directories of RES are almost always fixed, as can be seen every time: drawable-xxxx, layout, values, and uncommon: Anim, XML, row, color.
The Android textbook tells you:
res/drawable/used to store picture files;
res/layout/is used to store layout definition files;
Res/values/is used to store variables, parameters and other files.
This is what we already know. In addition, Android assigns IDs to all resources in the Res directory, and its main allocation principle is:
Picture files in drawable are always a resource ID for each file.
Each view that uses android:id= "@+id/xxx" in the XML file is assigned an unused resource ID.
Other more complex rules may be added by everyone.
In ADK's API, there are many ways to use resource IDs as parameters, such as the Getdrawable method:
Getresources (). getdrawable (R.DRAWABLE.SENDSMS_BK));
Literally, the Getresouces method returns a Android.content.res.Resources object.
The Getdrawalbe method returns a Drawable object, which we know is a picture.
The parameter used by the Getdrawable method is the resource ID.
But what exactly are the resource IDs for these int types? or where they're all put. If you're careful, you can find them in the Gen directory's R.java file. Each 16-binary integer ID has a very o-o attribute name, which are public static final, and it is recommended that you do not modify them manually. These resource IDs are in different class hierarchies, and you'll see some of these rules, for example, the Drawable class contains a resource ID that actually represents a picture file in the Res/drawable directory, and the resource ID contained in the layout class actually represents the Res/ The XML file in the layout directory; the resource ID contained in the ID class is actually a Widget object that represents the Android:id= "@+id/xxx" used in the layout XML file--if you're using "@+tyre/xxx", of course, When Android compiles, a class called Tyre is created in R.java.
From what we learned above, we find that the resource ID is "R." The beginning of a variable or attribute--because it is defined in the R class--this class is with Android. R is not the same r, they are not in the same package hierarchy. The former is the definition of the Android framework of its own resources, such as: Android. R.layout.simple_expandable_list_item_1, Android. R.layout.simple_expandable_list_item_2. Have you used any of these resources? Have time to try it. And the other R is our own R, which defines our own resource ID. We can use the resource ID in the way of object-oriented r.xxx.yyy, and of course we can use it directly with 16 binary numbers.
Resource IDs are so important in Android, so we can get resources just by getting the resource ID. Because in some cases, we can't use a constant as a resource ID (because we want to replace it with a variable), we can only get the resource ID dynamically in two ways:
1. Reflection
The reflection of Java is so powerful that we always think of it when we are desperate. Let's look at a piece of code:
Try {
Field field=r.drawable. class. GetField (type);
int i= field.getint (new r.drawable ());
Log. D (tag, i+ "");
return i;
}catch(Exception e) {
Log. e (tag, e.tostring ());
return R.drawable. Unknow;
}
Type is a variable whose possible values are "close", "Edit", "icon", and so on. We use the Reflection class field in Java to access a field below the R.drawable class. In fact, we know that the field under R.drawable is the resource ID of the picture file under the Res.drawable-xxxx directory. So running the above code is actually a string to get the corresponding image file resource ID. For example, when the value of type is "icon", the above code will get the resource ID of the Icon.png picture file in the Res.drawable-xxxx directory and return it to us. It is logical that we display the picture in the ImageView by the resource ID.
2. Getidentifier method using the Resources class
Using reflection is still a hassle. If the same thing is done with the android.content.res.Resources class, it is just two words:
Resources res=getresources ();
Return Res.getidentifier (Type, "drawable", Getpackagename ());
The Getresources method is derived from the 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 3 parameters: Field name, class name, package name. The package name specifies the fully qualified name of the package name section, if R is fully qualified named Android. R or COM.COMPANY.R, the package name here is "Android" or "Com.company". Getpackagename is actually this.getpackagename (), which returns the package name of this class directly.
The class name is the class to which the resource belongs. For example, we know that there are several fixed classes in the R.java class: Drawable, ID, string, layout, and so on, and many resource IDs are defined underneath them.
The field name is the name of the resource ID. For example, this resource ID definition: public static final int del=0x7f020002;
Del is the name of a resource ID, and 0x7f020002 is its 16 binary value.
With 3 parameters, the Getidentifier method can obtain a resource ID in a dynamic manner.
Android: Dynamic acquisition of resource ID and resource ID