Android中通过反射获取资源Id

开发SDK时,若项目中需要引用到res中的资源,那提供对接文件给其他项目引用时,Jar代码获取资源不能直接引用 R.xx.xx,这时候不能将资源一起打包,只能通过反射机制动态获取资源。


  • 1.通过 Resources.getIdentifierint 方法来进行资源ID的获取:

    android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)

封装成一个工具类 


package com.cp.utils;

import android.content.Context;

public class CPResourceUtil { 
 
    public static int getLayoutId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, "layout", 
                paramContext.getPackageName()); 
    } 
 
    public static int getStringId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, "string", 
                paramContext.getPackageName()); 
    } 
 
    public static int getDrawableId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, 
                "drawable", paramContext.getPackageName()); 
    } 
     
    public static int getStyleId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, 
                "style", paramContext.getPackageName()); 
    } 
     
    public static int getId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString,"id", paramContext.getPackageName()); 
    } 
     
    public static int getColorId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, 
                "color", paramContext.getPackageName()); 
    } 
    public static int getArrayId(Context paramContext, String paramString) { 
        return paramContext.getResources().getIdentifier(paramString, 
                "array", paramContext.getPackageName()); 
    } 
} 


 

资源的获取 

<string name="version_update_schedule">已下载%1$s请稍等&#8230;</string>

 

String text=mContext.getResources().getString(CPResourceUtil.getStringId(mContext, "version_update_schedule"));

 TextView.setText(String.format(text, p+"%"));

version_update_schedule 是string文件中name值

  • 2.通过反射来读取包名路径下的R$系列文件进行资源ID的获取:


[java]  view plain  copy
  1. public class ResoureExchange {  
  2.     private static final String TAG = ResoureExchange.class.getName();  
  3.     private static ResoureExchange self;  
  4.   
  5.     private Context mContext;  
  6.   
  7.     private static Class<?> CDrawable = null;  
  8.   
  9.     private static Class<?> CLayout = null;  
  10.   
  11.     private static Class<?> CId = null;  
  12.   
  13.     private static Class<?> CAnim = null;  
  14.   
  15.     private static Class<?> CStyle = null;  
  16.   
  17.     private static Class<?> CString = null;  
  18.   
  19.     private static Class<?> CArray = null;  
  20.   
  21.     public static ResoureExchange getInstance(Context context){  
  22.         if(self == null){  
  23.             self = new ResoureExchange(context);  
  24.         }  
  25.         return self;  
  26.     }  
  27.   
  28.     private ResoureExchange(Context context){  
  29.         this.mContext = context.getApplicationContext();  
  30.         try{  
  31.             CDrawable = Class.forName(this.mContext.getPackageName() + ".R$drawable");  
  32.             CLayout = Class.forName(this.mContext.getPackageName() + ".R$layout");  
  33.             CId = Class.forName(this.mContext.getPackageName() + ".R$id");  
  34.             CAnim = Class.forName(this.mContext.getPackageName() + ".R$anim");  
  35.             CStyle = Class.forName(this.mContext.getPackageName() + ".R$style");  
  36.             CString = Class.forName(this.mContext.getPackageName() + ".R$string");  
  37.             CArray = Class.forName(this.mContext.getPackageName() + ".R$array");  
  38.   
  39.         }catch(ClassNotFoundException e){  
  40.             Log.i(TAG,e.getMessage());  
  41.         }  
  42.     }  
  43.   
  44.     public int getDrawableId(String resName){  
  45.         return getResId(CDrawable,resName);  
  46.     }  
  47.       
  48.     public int getLayoutId(String resName){  
  49.         return getResId(CLayout,resName);  
  50.     }  
  51.       
  52.     public int getIdId(String resName){  
  53.         return getResId(CId,resName);  
  54.     }  
  55.       
  56.     public int getAnimId(String resName){  
  57.         return getResId(CAnim,resName);  
  58.     }  
  59.       
  60.     public int getStyleId(String resName){  
  61.         return getResId(CStyle,resName);  
  62.     }  
  63.       
  64.     public int getStringId(String resName){  
  65.         return getResId(CString,resName);  
  66.     }  
  67.       
  68.     public int getArrayId(String resName){  
  69.         return getResId(CArray,resName);  
  70.     }  
  71.       
  72.     private int getResId(Class<?> resClass,String resName){  
  73.         if(resClass == null){  
  74.             Log.i(TAG,"getRes(null," + resName + ")");  
  75.             throw new IllegalArgumentException("ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have " + this.mContext.getPackageName() + ".R$* configured in obfuscation. field=" + resName);  
  76.         }  
  77.   
  78.         try {  
  79.             Field field = resClass.getField(resName);  
  80.             return field.getInt(resName);  
  81.         } catch (Exception e) {  
  82.             Log.i(TAG, "getRes(" + resClass.getName() + ", " + resName + ")");  
  83.             Log.i(TAG, "Error getting resource. Make sure you have copied all resources (res/) from SDK to your project. ");  
  84.   
  85.             Log.i(TAG, e.getMessage());  
  86.         }   
  87.   
  88.         return -1;  
  89.     }  
  90. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值