閱讀678 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 通過字符串來獲取R下麵資源的ID 值

原文:https://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));  


最後更新:2017-04-02 06:52:14

  上一篇:go 抽象的力量
  下一篇:go 收藏個博客 爪哇米工作室陳躍峰的移動開發博客