在开发SDK中,如果需求中要开发者去实现界面,然后再给别人调用。这样的需求,我们必须去写一个工具类来获取各个资源文件夹下的id。通过底层获取id的代码实现,这样游戏接了才不会导致找不到资源ID。
import android.content.Context; import java.lang.reflect.Field; public class ResourceUtil { public ResourceUtil() { } public static int getLayoutResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "layout", context.getPackageName()); } public static int getIdResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "id", context.getPackageName()); } public static int getStringResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "string", context.getPackageName()); } public static int getDrawableResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "drawable", context.getPackageName()); } public static int getRawResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "raw", context.getPackageName()); } public static int getAnimResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "anim", context.getPackageName()); } public static int getStyleResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "style", context.getPackageName()); } public static int getColorResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "color", context.getPackageName()); } public static int getIntegerResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "integer", context.getPackageName()); } public static int getBoolResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "bool", context.getPackageName()); } public static int getDimenResIDByName(Context context, String name) { return context.getResources().getIdentifier(name, "dimen", context.getPackageName()); } private static Object getResourceId(Context context, String name, String type) { String className = context.getPackageName() + ".R"; try { Class e = Class.forName(className); Class[] var8; int var7 = (var8 = e.getClasses()).length; for(int var6 = 0; var6 < var7; ++var6) { Class childClass = var8[var6]; String simple = childClass.getSimpleName(); if(simple.equals(type)) { Field[] var13; int var12 = (var13 = childClass.getFields()).length; for(int var11 = 0; var11 < var12; ++var11) { Field field = var13[var11]; String fieldName = field.getName(); if(fieldName.equals(name)) { System.out.println(fieldName); return field.get((Object)null); } } } } } catch (Exception var15) { var15.printStackTrace(); } return null; } public static int getStyleableResIDByName(Context context, String name) { return ((Integer)getResourceId(context, name, "styleable")).intValue(); } public static int[] getStyleableArrayResIDByName(Context context, String name) { return (int[])getResourceId(context, name, "styleable"); } }
本文介绍了一个用于Android开发的资源ID获取工具类ResourceUtil。该工具类提供了多种方法,如获取布局、字符串、图片等资源ID,帮助开发者在不直接操作资源文件的情况下轻松获取所需资源ID。
150

被折叠的 条评论
为什么被折叠?



