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());
    }
}
相关推荐
程序员陆业聪4 小时前
从 OpenClaw 到 Android:Harness Engineering 是怎么让 Agent 变得可用的
android
hnlgzb6 小时前
常见的Android Jetpack库会有哪些?这些库中又有哪些常用类的?
android·android jetpack
钛态9 小时前
Flutter 三方库 http_mock_adapter — 赋能鸿蒙应用开发的高效率网络接口 Mock 与自动化测试注入引擎(适配鸿蒙 HarmonyOS Next ohos)
android·网络协议·flutter·http·华为·中间件·harmonyos
王码码20359 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
左手厨刀右手茼蒿9 小时前
Flutter for OpenHarmony: Flutter 三方库 shamsi_date 助力鸿蒙应用精准适配波斯历法(中东出海必备)
android·flutter·ui·华为·自动化·harmonyos
代码飞天9 小时前
wireshark的高级使用
android·java·wireshark
2501_9159184110 小时前
苹果App Store上架审核卡住原因分析与解决方案指南
android·ios·小程序·https·uni-app·iphone·webview
skiy11 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
小小小点11 小时前
Android四大常用布局详解与实战
android
MinQ12 小时前
binder和socket区别及原理
android