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());
    }
}
相关推荐
Lei活在当下2 小时前
【Perfetto从入门到精通】4.使用 heapprofd 工具采样追踪 Java/Native 内存分配
android·性能优化·架构
alexhilton3 小时前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
summerkissyou19875 小时前
Android-Camera-为啥不移到packages/module
android·相机
liang_jy5 小时前
Android UID
android·面试
nono牛7 小时前
安卓/MTK平台日志关键词详解
android
TimeFine8 小时前
Android AI解放生产力(四)实战:解放绘制UI的繁琐工作
android
sheji34169 小时前
【开题答辩全过程】以 基于Android的社区车位共享管理系统的设计与实现为例,包含答辩的问题和答案
android
TimeFine9 小时前
Android AI解放生产力(三):认识custom_prompts和skills
android
summerkissyou19879 小时前
Android-Audio-为啥不移到packages/module
android·音视频
catchadmin9 小时前
PHP 值对象实战指南:避免原始类型偏执
android·开发语言·php