Android resource id details
We usually use the findViewById method to obtain resources. 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 obtain various View objects in layout such as buttons, labels, ListView and ImageView. As the name implies, it requires an int parameter: resource id.
Resource id is very useful. Android automatically assigns id to each resource located in the res directory, including various image files and "@+id" objects in the XML text. The subdirectory of res is almost always fixed, such as the ones you can see every time: drawable-xxxx, layout, values, and uncommon: anim, xml, row, 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 some variables, parameters and other files.
This is all we already know. In addition, Android will assign ids to all resources in the res directory, and its main allocation principles are:
The image files in drawable are always one resource id per file.
Each view in the Xml file that uses android:id=”@+id/xxx” will be assigned an unused resource id.
Maybe everyone will add some other more complex rules.
In the ADK API, there are many methods that actually use the resource id as a parameter.
For example, getDrawable method:
getResources().getDrawable(R.drawable.sendsms_bk));
Literally, the getResources method returns an android.content.res.Resources object.
The getDrawalbe method returns a Drawable object, we know that this is an image.
The parameter used by the getDrawable method is the resource id.
However, what numbers are these int resource ids? Or where they are all placed. If you are careful enough, you can find them in the R.Java file in the gen directory. Each hexadecimal integer id has a very OO attribute name, they are all public static final, and it is recommended that you do not modify them manually. These resource ids are located in different class hierarchies. If you look carefully, you will find some of the rules. For example, the resource id in the drawable class actually represents the image file 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 widget object using android:id="@+id/xxx" in the layout xml file - of course, if you use "@+Tyre/xxx", a class called Tyre will be created in R.java when compiling.
Through the above understanding, we found that resource id is a variable or property that starts with "R." - because it is all defined in the R class - this class is not the same R as android.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, android.R.layout.simple_expandable_list_item_2. Have you used these resources? If you have time, you might as well give it a try. The other R is our own R, which defines our own resource id. We can use resource ids in object-oriented R.xxx.yyy, and of course we can use them directly with hexadecimal numbers.
Resource id is so important in Android, so we can get the resource as long as we obtain the resource id. Because in some cases, we cannot use a constant as the resource id (because we want to replace it with a variable), we can only dynamically obtain the resource id in two ways:
1. Reflection
Java's reflection 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, and its possible values will be "close", "edit", "icon", etc. We use Java's reflection class Field 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 image file in the res.drawable-xxxx directory. Therefore, running the above code is actually to obtain the resource id of the corresponding image file through a string. For example, when the value of type is "icon", the above code will obtain the resource id of the icon.png image file in the res.drawable-xxxx directory and return it to us. It is natural for us to display the image in the ImageView through the resource id.
2. Use the getIdentifier method of the Resources class
Using reflection is still a hassle. If you use the android.content.res.Resources class to do the same thing, it's just two sentences:
Resources res=getResources(); return res.getIdentifier(type,"drawable",getPackageName());
The getResources method comes from the contextxt (that is, the Activity class), which can directly return a Resources object. The getIdentifier method of Resources can return any resource id in R.java. Of course, you must specify 3 parameters: field name, class name, and package name. The package name part of the fully qualified name specified by the package name. If the fully qualified 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. For example, as we know, several fixed classes in the R.java class: drawable, id, string, layout, etc., and many resource ids are defined below them.
The field name is the name of the resource id. For example, the resource id definition:
public static final int del=0x7f020002;
del is the name of a resource id, and 0x7f020002 is its hexadecimal value.
With 3 parameters, the getIdentifier method can obtain the resource id by comparing dynamic methods.
Thank you for reading, I hope it can help you. Thank you for your support for this site!