有时候我们的需求需要用到大量的资源文件,我们可以根据资源的名字来获取对应的资源文件,避免重复造轮子,可用如下方法:
/** * 根据文件名称获取资源id * @param fileName * @return */ public static int getResourceId(Context context,String fileName) { try { int id = context.getResources().getIdentifier(fileName, "mipmap", context.getPackageName()); return id; } catch (Exception e) { } return 0; }
其中的mipmap可换成drawable。