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());
    }
}
相关推荐
androidwork9 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空9 小时前
Android第十三次面试总结基础
android·面试·职场和发展
wu_android9 小时前
Android 相对布局管理器(RelativeLayout)
android
李斯维11 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork11 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
像风一样自由11 小时前
【001】frida API分类 总览
android·frida
casual_clover11 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
移动开发者1号13 小时前
Android 大文件分块上传实战:突破表单数据限制的完整方案
android·java·kotlin
移动开发者1号13 小时前
单线程模型中消息机制解析
android·kotlin
每次的天空16 小时前
Android第十五次面试总结(第三方组件和adb命令)
android