Android中根据字符串动态获取资源文件ID

有时候想在代码运行的时候根据资源名称去获取id从而使用调用资源文件。

Resource中的getIdentifier()可以解决这个问题

java 复制代码
    public int getIdentifier(String name, String defType, String defPackage) {
        return mResourcesImpl.getIdentifier(name, defType, defPackage);
    }

使用方法:

java 复制代码
   int resId = context.getResources().getIdentifier(paramString,
          "mipmap", context.getPackageName());

除了上面案例中的mipmap可以这样使用之后,以下类型的都可以

  • layout
  • string
  • array
  • drawable
  • style
  • id
  • color
  • attr
  • styleable
  • mipmap

封装工具类

java 复制代码
public class ResourceUtil {
 
	public static int getLayoutId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName, "layout",  
                context.getPackageName());  
    }  
  
    public static int getStringId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName, "string",  
                context.getPackageName());  
    }
 
    public static int getArrayId(Context context, String resName) {
        return context.getResources().getIdentifier(resName, "array",
                context.getPackageName());
    }
 
    public static int getDrawableId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName,  
                "drawable", context.getPackageName());  
    }  
      
    public static int getStyleId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName,  
                "style", context.getPackageName());  
    }  
      
    public static int getId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName,  
                "id", context.getPackageName());  
    }  
      
    public static int getColorId(Context context, String resName) {  
        return context.getResources().getIdentifier(resName,  
                "color", context.getPackageName());  
    }
 
    public static int getAttrId(Context context, String resName) {
        return context.getResources().getIdentifier(resName,
                "attr", context.getPackageName());
    }
 
    public static int getStyleableId(Context context, String resName) {
        return context.getResources().getIdentifier(resName,
                "styleable", context.getPackageName());
    }

 
    public static int getMipmapId(Context context, String resName) {
        return context.getResources().getIdentifier(resName,
                "mipmap", context.getPackageName());
    }
}
相关推荐
小袁拒绝摆烂2 小时前
SQL开窗函数
android·sql·性能优化
apihz3 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
baidu_247438613 小时前
Android MPAndroidChart使用
android
天平4 小时前
react native现代化组件库的推荐 【持续更新...】
android·前端·react native
apihz5 小时前
通用图片搜索-搜狗源免费API接口使用指南
android·java·python·php·音视频
你过来啊你5 小时前
Android开发中ARouter使用和原理详解
android
apihz7 小时前
腾讯云轻量服务器创建快照免费API接口教程
android·服务器·数据库·python·网络协议·tcp/ip·腾讯云
Kiri霧8 小时前
Noting
android·开发语言·kotlin
东风西巷13 小时前
NealFun安卓版:创意无限,娱乐至上
android·人工智能·智能手机·娱乐·软件需求
小李飞飞砖19 小时前
Sophix、Tinker 和 Robust 三大主流 Android 热修复框架的详细对比
android