方法一:
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());
} 方法二:
//把字符串转换为资源Id
public static int getResourdIdByResourdName(Context context, String defType, String ResName){
Resources res = context.getResources();
int resourceId = res.getIdentifier(ResName,defType,context.getPackageName());
return resourceId;
}
本文介绍了两种在Android中获取资源ID的方法,并提供了相应的代码示例。第一种方法使用Java反射API,第二种方法通过资源名获取资源ID。

被折叠的 条评论
为什么被折叠?



