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

原文:http://www.oschina.net/code/snippet_163910_6305

获取drawable下的icon的值。


方法一:

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


方法二:

Resources res=getResources();  
int i=res.getIdentifier("icon","drawable",getPackageName());  

Log.i("res",Integer.toString(i));  


你可能感兴趣的:(Android 通过字符串来获取R下面资源的ID 值)