1.常用方法
public int getResId(String name,Context context){
Resources r = context.getResources();
int id = r.getIdentifier(name,"drawable","com.demo");
return id;
}
2.使用反射(推荐,性能高)
public class ResourceMan {
public static int getResId(String variableName, Class<?> c) {
try {
Field idField = c.getDeclaredField(variableName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}

使用方法:

int id = ResourceMan.getResId("icon",R.drawable.class);

最新文章

  1. MySQL5.0安装图解
  2. escape(), encodeURI()和encodeURIComponent()(转)
  3. centos上libreoffice+unoconv安装步骤,实现word转pdf
  4. .Net控件经验集合
  5. 如何让django方法自动地定期执行
  6. php开发(TP框架使用)
  7. Linux cscope命令
  8. windows7 密码保护 共享文件
  9. codeforces round367 div2.C (DP)
  10. Unable to resolve target 'android-8'类似错误的解决办法
  11. 有关TCP和UDP 粘包 消息保护边界
  12. GitHub上线Trending功能,帮你轻松找到有潜力的开源项目
  13. Spring 学习笔记01
  14. PowerShell_零基础自学课程_3_如何利用Powershell ISE调试PS脚本
  15. Android Studio 代码混淆
  16. Hybrid----U采用IWebView演出PDF和其他文件
  17. Transform介绍(Unity3D开发之二)
  18. css边框的一些属性
  19. stock
  20. UITableView详解

热门文章

  1. 本地创建Duplicate数据库
  2. 【hdu5419】Victor and Toys
  3. The literal of int xxxxx is out of range
  4. MongoDB的CURD命令
  5. 《TCP-IP详解卷2:实现》【PDF】下载
  6. 【java】彩票中奖码生成器:java.util.Random里的方法public int nextInt(int bound)
  7. 【java】Vector
  8. android中的五大布局(控件的容器,可以放button等控件)
  9. 向map中追加元素
  10. tr 命令详解