本文从其他博客摘录,我们常碰到将程式打包成jar方式发给别人调用,但在获取资源时用普的R.id方式会导致报异常(找不到)
public class Helper
{
public static int getLayoutId(Context context ,String name)
{
return context.getResources().getIdentifier(name, "layout", context.getPackageName());
}
public static int getResId(Context context ,String name)
{
return context.getResources().getIdentifier(name,"id",context.getPackageName());
}
public static int getResDraw(Context context ,String name)
{
return context.getResources().getIdentifier(name,"drawable",context.getPackageName());
}
public static int getResStyle(Context context ,String name)
{
return context.getResources().getIdentifier(name,"style",context.