标签:
我们需要把代码里面的R.xx.xx改成以下方式读取:
1 package com.xlcw.sdk.tool; 2 3 import java.lang.reflect.Field; 4 import android.content.Context; 5 import android.util.Log; 6 7 public class GetItemId{ 8 9 public static Context paramContext; 10 11 public static void setContext(Context paramContext) 12 { 13 GetItemId.paramContext = paramContext; 14 } 15 16 public static int getItemId(String paramString1, 17 String paramString2) { 18 try { 19 Class<?> localClass = Class.forName(paramContext.getPackageName() 20 + ".R$" + paramString1); 21 Field localField = localClass.getField(paramString2); 22 int i = Integer.parseInt(localField.get(localField.getName()) 23 .toString()); 24 return i; 25 } catch (Exception localException) { 26 Log.e("getIdByReflection error", localException.getMessage()); 27 } 28 return 0; 29 } 30 31 public static int[] getItemIdArray(String paramString1, String paramString2) { 32 try { 33 Class<?> localClass = Class.forName(paramContext.getPackageName() 34 + ".R$" + paramString1); 35 Field localField = localClass.getField(paramString2); 36 int[] i = (int[]) localField.get(localField.getName().toString()); 37 return i; 38 } catch (Exception localException) { 39 Log.e("getIdByReflection error", localException.getMessage()); 40 } 41 return null; 42 } 43 44 public static int getLayoutResIDByName(String name) { 45 return paramContext.getResources().getIdentifier(name, "layout", 46 paramContext.getPackageName()); 47 } 48 49 public static int getIdResIDByName(String name) { 50 return paramContext.getResources().getIdentifier(name, "id", 51 paramContext.getPackageName()); 52 } 53 54 public static int getStringResIDByName(String name) { 55 return paramContext.getResources().getIdentifier(name, "string", 56 paramContext.getPackageName()); 57 } 58 59 public static int getDrawableResIDByName(String name) { 60 return paramContext.getResources().getIdentifier(name, "drawable", 61 paramContext.getPackageName()); 62 } 63 64 }
如何把使用到android res文件夹下面资源(R.xx.xx)的工程打包成jar文件,供其它项目使用
标签:
原文地址:http://www.cnblogs.com/xiaoxiangmomo/p/4982932.html