Android 根据字符串动态获取资源ID
2024-08-23 01:49:34
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);
最新文章
- MySQL5.0安装图解
- escape(), encodeURI()和encodeURIComponent()(转)
- centos上libreoffice+unoconv安装步骤,实现word转pdf
- .Net控件经验集合
- 如何让django方法自动地定期执行
- php开发(TP框架使用)
- Linux cscope命令
- windows7 密码保护 共享文件
- codeforces round367 div2.C (DP)
- Unable to resolve target 'android-8'类似错误的解决办法
- 有关TCP和UDP 粘包 消息保护边界
- GitHub上线Trending功能,帮你轻松找到有潜力的开源项目
- Spring 学习笔记01
- PowerShell_零基础自学课程_3_如何利用Powershell ISE调试PS脚本
- Android Studio 代码混淆
- Hybrid----U采用IWebView演出PDF和其他文件
- Transform介绍(Unity3D开发之二)
- css边框的一些属性
- stock
- UITableView详解