android通过反射获得资源数据
来源:互联网 发布:中国旅游业发展数据 编辑:程序博客网 时间:2024/06/05 23:02
import android.content.Context;
import android.content.res.Resources;
import com.zlongame.utils.LogUtils.PDLog;
/**
* 在不能使用id的情况下 通过使用反射获取数据
*/
public class ResourcesUtil {
private static String mPackageName;//接入游戏的包名
private static Resources mResources;//接入游戏的包名
public static void init(Context context){ mPackageName=context.getApplicationContext().getPackageName(); mResources=context.getApplicationContext().getResources();}public static int getLayout(String resName){ return mResources.getIdentifier(resName, "layout", mPackageName);}public static int getDrawable(String resName){ return mResources.getIdentifier(resName, "drawable", mPackageName);}public static int getString(String resName){ return mResources.getIdentifier(resName, "string", mPackageName);}public static int getId(String resName){ return mResources.getIdentifier(resName, "id", mPackageName);}public static int getStyle(String resName){ return mResources.getIdentifier(resName, "style", mPackageName);}public static int getArray(String resName){ return mResources.getIdentifier(resName, "array", mPackageName);}public static int getColor(String resName){ return mResources.getIdentifier(resName, "color", mPackageName);}public static int getDimen(String resName){ return mResources.getIdentifier(resName, "dimen", mPackageName);}public static int getAnim(String resName){ return mResources.getIdentifier(resName, "anim", mPackageName);}public static int getRaw(String resName){ return mResources.getIdentifier(resName, "raw", mPackageName);}public static int getStyleable(String resName){ return mResources.getIdentifier(resName, "styleable", mPackageName);}public static int[] getStyleableArray(String resName){ return getResourceIDsByName(resName,"styleable",mPackageName);}/** * 利用反射,获取int数组格式的资源ID,例如styleable */private static int[] getResourceIDsByName(String resName,String resType,String packageName){ Class clsR = null; int[] ids = null; try { clsR = Class.forName(packageName + ".R"); Class[] classes = clsR.getClasses(); Class desClass = null; for(int i=0;i<classes.length;i++){ String[] temp = classes[i].getName().split("\\$"); if(temp.length >= 2){ if(temp[1].equals(resType)){ desClass = classes[i]; break; } } } if(desClass != null){ ids = (int[])desClass.getField(resName).get(resName); } } catch (Exception e) { PDLog.e(e); } return ids;}
}
0 0
- android通过反射获得资源数据
- Android中通过反射获取资源Id
- Android中通过反射获取资源Id
- Android通过反射动态获取资源Id
- Android中通过反射获取资源Id
- android 通过资源名称去获得资源R id
- android通过资源名称去获得资源ID
- android 通过占位符来动态获得字符串资源
- android开发 通过class.getResourceAsStream()方法 获得资源。
- Android 通过反射综合应用-获取插件Plugin资源
- android通过反射获取资源ID的工具类
- android获得asset资源
- Android 获得图片资源
- Android中使用反射通过资源名字找到资源ID的方法
- android中通过id获得drawable类型的数据
- android反射获取资源
- android 资源反射备忘
- 通过反射获得泛型信息
- sqlldr 导入乱码,Oracle客户端字符集问题
- SQL删除重复数据方法
- armv6、armv7、armv7s及arm64
- android中获取当前Activity的实例和名字
- CSS实现水平垂直同时居中的5种思路
- android通过反射获得资源数据
- Android前台界面之详解TextView内容任意长度的伸缩、显示与隐藏
- NOIP2005题解
- 深度学习数据集
- collectionView - flowLayout
- IE8下关于indexOf和for in循环的问题
- vc++6.0中的图形图像处理
- 网络字节序与主机字节序
- 【VS开发】修改窗口背景颜色大全