怎么把一个字符串转化为资源的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");
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");
相关文章推荐
- Java怎么将一个含有逻辑运算符的字符串转化成逻辑表达式
- 已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”
- 已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”。
- 怎么将一个16进制字符串转化为10进制输出?
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
- 已知字符串msg='get-element-by-id';,写一个函数将其转化成驼峰表示法:'getElementById'
- js 中如何把一个普通的字符串转化为数组对象呢
- 怎么判断字符串中是否包含一组字符的任意一个字符及判断字符串是否只含字母和数字
- C++将一个数值转化为二进制字符串
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- 怎么通过一个字符串初始化一个对象
- android 通过字符串来获取R下面资源的ID 值
- 在JS的脚本里,怎么把一个html字符串转成的DOM对象.从而能使用返回的DOM对象来访问某一个节点?
- 怎么把一个字符串转换为汉字
- java把字符串转化为二进制,再把二进制转换成字符串的一个小例子
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- 相同的ID,按时间排序取最大的一个值怎么取
- List集合转化为自定义规则的字符串,List集合序列化为一个String 字符串(规则自己定)
- Struts1框架五之怎么自定义一个FormBean的类型转化器
- c++将一个字符串转化为指定进制的数