Android 通过字符串来获取R下面资源的ID 值
方法一:
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)); |