正文  基础知识 > 资源/Resource >

Android 通过字符串来获取R下面资源的ID 值

获取drawable下的icon的值。[代码]方法一:1try{2Field field=R.drawable.class.getField("icon");3inti= field.getInt(newR.drawable());4Log.d("icon",i+"");5}catch(Exception e){6Log.e("icon",e.toStrin......

获取drawable下的icon的值。

[代码] 方法一:

1 try{ 
2  Field field=R.drawable.class.getField("icon"); 
3  int i= field.getInt(new R.drawable()); 
4   Log.d("icon",i+""); 
5 }catch(Exception e){ 
6  Log.e("icon",e.toString()); 
7 }

[代码] 方法二:

1 Resources res=getResources(); 
2 int i=res.getIdentifier("icon","drawable",getPackageName()); 
3  
4 Log.i("res",Integer.toString(i));