您的位置:首页 > 移动开发 > Android开发

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

2013-08-09 12:18 330 查看
方法一:

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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: