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());
    }
}
相关推荐
冬奇Lab4 小时前
PowerManagerService(下):Doze模式与电池优化
android·源码阅读
砖厂小工6 小时前
Compose 中函数引用 vs Lambda:到底该用哪个?
android
Kapaseker16 小时前
详解 Compose background 的重组陷阱
android·kotlin
黄林晴16 小时前
Kotlin 2.3.20-RC2 来了!JPA 开发者狂喜,6 大更新一文速览
android·kotlin
kymjs张涛1 天前
OpenClaw 学习小组:初识
android·linux·人工智能
范特西林1 天前
实战演练——从零实现一个高性能 Binder 服务
android
范特西林1 天前
代码的生成:AIDL 编译器与 Parcel 的序列化艺术
android
范特西林1 天前
深入内核:Binder 驱动的内存管理与事务调度
android
范特西林1 天前
解剖麻雀:Binder 通信的整体架构全景图
android
范特西林1 天前
破冰之旅:为什么 Android 选择了 Binder?
android