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

来源:互联网 发布:windows浏览器排名 编辑:程序博客网 时间:2024/04/28 19:45

1. [代码]方法一:      

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

2. [代码]方法二:     

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

0 0