java res_通过代码获取res里生成R.java中的值

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值