通过代码获取res里生成R.java中的值
2013-07-03 10:25
369 查看
引用:http://my.eoe.cn/blue_rain/archive/552.html
有的时候我们生成库文件,需要在里面加入一些UI,并提供出一些xml的资源,那如何在其他项目中使用呢?
我们只需要在需要生成库文件的代码中不要直接是用R. ,而是使用自己写的方法获取。
下面上代码:
使用方法:在要生成库文件的Java文件中,使用
有的时候我们生成库文件,需要在里面加入一些UI,并提供出一些xml的资源,那如何在其他项目中使用呢?
我们只需要在需要生成库文件的代码中不要直接是用R. ,而是使用自己写的方法获取。
下面上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import java.lang.reflect.Field; import android.content.Context; import android.util.Log; public class ResUtil { private static final String TAG = ResUtil.class.getName(); private static ResUtil instance; private Context context; private static Class id= null; private static Class drawable = null; private static Class layout = null; private static Class anim = null; private static Class style = null; private static Class string = null; private static Class array = null; private ResUtil(Context paramContext) { this.context = paramContext.getApplicationContext(); try { drawable = Class.forName(this.context.getPackageName() + ".R$drawable"); } catch (ClassNotFoundException localClassNotFoundException1) { Log.i(TAG, localClassNotFoundException1.getMessage()); } try { layout = Class.forName(this.context.getPackageName() + ".R$layout"); } catch (ClassNotFoundException localClassNotFoundException2) { Log.i(TAG, localClassNotFoundException2.getMessage()); } try { id = Class.forName(this.context.getPackageName() + ".R$id"); } catch (ClassNotFoundException localClassNotFoundException3) { Log.i(TAG, localClassNotFoundException3.getMessage()); } try { anim = Class.forName(this.context.getPackageName() + ".R$anim"); } catch (ClassNotFoundException localClassNotFoundException4) { Log.i(TAG, localClassNotFoundException4.getMessage()); } try { style = Class.forName(this.context.getPackageName() + ".R$style"); } catch (ClassNotFoundException localClassNotFoundException5) { Log.d(TAG, localClassNotFoundException5.getMessage()); } try { string = Class.forName(this.context.getPackageName() + ".R$string"); } catch (ClassNotFoundException localClassNotFoundException6) { Log.d(TAG, localClassNotFoundException6.getMessage()); } try { array = Class.forName(this.context.getPackageName() + ".R$array"); } catch (ClassNotFoundException localClassNotFoundException7) { Log.d(TAG, localClassNotFoundException7.getMessage()); } } public static ResUtil getResofR(Context paramContext) { if (instance == null) instance = new ResUtil(paramContext); return instance; } public int getAnim(String paramString) { return getResofR(anim, paramString); } public int getId(String paramString) { return getResofR(id, paramString); } public int getDrawable(String paramString) { return getResofR(drawable, paramString); } public int getLayout(String paramString) { return getResofR(layout, paramString); } public int getStyle(String paramString) { return getResofR(style, paramString); } public int getString(String paramString) { return getResofR(string, paramString); } public int getArray(String paramString) { return getResofR(array, paramString); } private int getResofR(Class<?> paramClass, String paramString) { if (paramClass == null) { Log.d(TAG, "getRes(null," + paramString + ")"); throw new IllegalArgumentException("ResClass is not initialized."); } try { Field localField = paramClass.getField(paramString); int k = localField.getInt(paramString); return k; } catch (Exception localException) { Log.d(TAG, "getRes(" + paramClass.getName() + ", " + paramString + ")"); Log.d(TAG, "Error getting resource. Make sure you have copied all resources (res/) from SDK to your project. "); Log.d(TAG, localException.getMessage()); } return -1; } } |
1 2 | View view = getLayoutInflater().inflate(ResUtil.getResofR(this).getLayout("activity_main"), null); set |
相关文章推荐
- Android 通过Java代码动态获取控件位置的终极解决之道
- 通过基站信息(LAC,CID)调用google的json接口获取地理位置信息的Java代码
- 通过Java进行网页抓取并生成plist创建代码
- java通过代码获取音频的时长
- java获取客户端ip 及通过ip获取mac地址后台代码(借鉴别人)
- 通过java代码生成XML格式文件
- java 两个通过java代码操作FTP的类,上传下载删除,获取文件
- Webservice实践(八)如何通过WSDL获取java代码
- java代码通过freemarker模板实现自动生成dao,model,service,jsp
- java 生成证书和签发证书(纯代码,编译通过)
- 微信公众号开发-开发环境搭建并通过java代码获取微信access_token
- c++代码通过JNI调用Java代码时用到的签名信息如何获取
- NativeCode中通过JNI反射调用Java层的代码,以获取IMEI为例
- SSH笔记 在JSP页面中通过Java获取系统时间并赋值给当前页面标签完整代码
- StartUML反向(逆向)Java工程通过代码生成类图
- cocos2d-x 使用JniHelper 调用 java代码 获取安卓生成的唯一标示UUID
- 把mysql数据库中的表通过java代码生成实体类
- Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解
- 通过wsdl2java生成客服端代码