android 获取资源id的工具类
来源:互联网 发布:淘宝3c数码类目包括 编辑:程序博客网 时间:2024/06/03 21:54
在开发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"); }}
阅读全文
0 0
- android 获取资源id的工具类
- android通过反射获取资源ID的工具类
- Android反射获取资源 id通用工具类
- Android 资源Id工具类
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源id及资源id的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- android 资源id及资源id的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- Android:资源 id 及资源 id 的动态获取
- 砝码称重问题
- h5 23
- 11-散列4 Hashing
- h5 24
- h5 25
- android 获取资源id的工具类
- 判断当前访问的设备是否是移动设备
- h5 25
- 快速排序算法
- jdbc-连接池的简单使用
- (99)蓝图数组
- nginx + tomcat + memcached 负载均衡动静分离技术
- 如何让hive支持多字符分割列
- h5 26