您的位置:首页 > 其它

怎么把一个字符串转化为资源的id?

2011-11-03 12:21 218 查看
public static int getResourdIdByResourdName(Context context, String
ResName){

int resourceId = 0;

try {

Field field = R.drawable.class.getField(ResName);

field.setAccessible(true);

try {

resourceId = field.getInt(null);

} catch (IllegalArgumentException e) {

log.showLogDebug("IllegalArgumentException:" + e.toString());

} catch (IllegalAccessException e) {

log.showLogDebug("IllegalAccessException:" + e.toString());

}

} catch (NoSuchFieldException e) {

log.showLogDebug("NoSuchFieldException:" + e.toString());

}

return resourceId;

}

调用:

int resId = CommonUtil.getResourdIdByResourdName(mContext, "btn_new");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐