This article describes how Android uses Getidentifier () to get a resource ID. Share to everyone for your reference, specific as follows:
int i= getresources (). Getidentifier ("icon", "drawable", Getpackagename ());
if (i>0)
{log.i ("AA", "AA");}
else
{log.i ("VBV", "AA");}
Or:
int resid = Getresources (). Getidentifier ("Org.loveandroid.androidtest:drawable/gallery_photo_1", null,null);
int resid = Getresources (). Getidentifier ("Org.anddev.android.testproject:drawable/bug", NULL, NULL);
or int resid = Getresources (). Getidentifier ("Bug", "drawable", "Org.anddev.android.testproject"); First argument: full_package:type/filename_without_ending is this format and then the other can be null int idflag = Getresources (). Getidentifier (
Getpackagename () + ":d Rawable/flag", NULL, NULL);
or int idflag = Getresources (). Getidentifier ("Flag", "drawable", Getpackagename ());
var drawable[] dw = new DRAWABLE[10];
for (int i = 1; I <= i++) {int id = getresources (). Getidentifier ("flag" + I, "drawable", Getpackagename ());
Dw[i-1] = Getresources (). getdrawable (ID); ///The reflection method can get all resources private void _dumpallresourceids (Class<?> ClassType) throws IllegalArgumentException {Field
[] fids = Classtype.getfields ();
try {for (int i = 0; i < fids.length i++) {Field fld = fids[i]; int NID =Fld.getint (NULL);
LOG.D ("Dbg", Classtype.getsimplename () + "" + i + ":" + fld.getname () + "=" + NID);
} catch (Exception e) {throw new illegalargumentexception ();
}} import Java.lang.reflect.Field;
... _dumpallresourceids (r.layout.class);
_dumpallresourceids (R.drawable.class);
Results:
R$layout 0:main=2130903040
R$layout 1:small_spinner_dropdown_item=2130903041
R$drawable 0:icon=2130837504
Sometimes we need to dynamically get the ID of a single control, and then do the operation, through the online lookup, found a way
Getresources (). Getidentifier ("textView01", "id", "cn.xxx.xxx");
The first parameter is the ID name, the second is the ID for the resource property, or drawable, and the third is the package name.
A problem is encountered during the project, read the picture name from the database, and then call the picture. Directly with R.drawable.? cannot be invoked. Search a lot of places finally found a way to share to everyone, hope to help.
Mainly by two methods, the individual suggests the second.
1. Do not put the picture under the res/drawable, but stored in a package of SRC (such as: Com.drawable.resource), in this case the method of invocation is:
String Path = "Com/drawable/resource/imagename.png";
InputStream is = getClassLoader (). getResourceAsStream (path);
Drawable.createfromstream (IS, "src");
2. If you still want to use the picture in res/drawable directly, you need to go through the following methods:
Suppose when creating the project, the package name is: Com.test.image
int resid = Getresources (). Getidentifier ("imagename", "drawable", "com.test.image");
drawable image = Getresources (). getdrawable (RESID);
For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with the Android program.