利用反射 获取android布局文件中设置的属性值
来源:互联网 发布:软件企业网址 编辑:程序博客网 时间:2024/05/16 18:50
private void initSystemAttrs(Context context, AttributeSet attrs) { if (attrs == null) { return; } Class<?> clazz = ReflectUtils.getClass("com.android.internal.R$styleable"); if (clazz == null) { return; } int[] getViewGroupStyleable = getStaticIntArrayField(clazz, "ViewGroup_Layout"); if (getViewGroupStyleable == null) { return; } try { TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attrs, getViewGroupStyleable); int layoutWidthIndex = getStaticIntField(clazz, "ViewGroup_Layout_layout_width"); int layoutHeightIndex = getStaticIntField(clazz, "ViewGroup_Layout_layout_height"); int layout_width= obtainStyledAttributes.getLayoutDimension(layoutWidthIndex, "layout_width"); int layout_height= obtainStyledAttributes.getLayoutDimension(layoutHeightIndex, "layout_height"); obtainStyledAttributes.recycle(); } catch (Exception e) { LogUtils.e(e); } } public static Class<?> getClass(String className) { try { Class<?> forName = Class.forName(className); return forName; } catch (Exception e) { LogUtils.e(e); } return null; } public static int[] getStaticIntArrayField(Class<?> clazz, String fieldName) { try { Field declaredField = clazz.getDeclaredField(fieldName); declaredField.setAccessible(true); Object object = declaredField.get(null); int[] result = (int[]) object; return result; } catch (Exception e) { LogUtils.e(e); } return null; } public static int getStaticIntField(Class<?> clazz, String fieldName) { try { Field declaredField = clazz.getDeclaredField(fieldName); declaredField.setAccessible(true); Object object = declaredField.get(null); int result = (int) object; return result; } catch (Exception e) { LogUtils.e(e); } return Integer.MIN_VALUE; }
0 0
- 利用反射 获取android布局文件中设置的属性值
- Android 应用利用反射机制获取/设置系统属性值
- C#利用反射获取属性的值
- Android 相对布局中的 代码中修改属性与布局文件的设置不同的解决方法
- 利用反射机制获取android系统属性值
- c#中如何利用反射设置属性值
- Android布局文件中常用的属性
- 利用反射获取与修改private的属性的值
- 利用反射获取类的属性名称
- Android布局文件中控件的高度与宽度属性设置
- C#利用反射获取对象属性值
- C#利用反射获取对象属性值
- 学习Android利用反射获取资源文件
- android代码中动态设置布局中控件的属性
- android代码中动态设置布局中控件的属性
- Java通过反射获取和设置对象的属性值
- Android布局文件的属性值解析
- Android布局文件的属性值解析
- 蓝牙 AVCTP 剖析
- Retrofit+RxJava在MVP模式中优雅地处理异常
- C++ 回调函数 --函数的接口 讲解
- 天梯赛L2-006树的遍历---已知中序后序---输出层序遍历
- 如何完成从科研人员到初创企业 CEO 的转型?
- 利用反射 获取android布局文件中设置的属性值
- 拍照上传
- mysql导出数据库表结构以及数据
- HDCP学习
- Linux中安装PHP的ZendGuardLoader扩展的详细解决方案
- 面向对象
- 查找(3)——二叉排序树的建立、结点的查找和删除
- Charles 抓包
- 使用NTP服务器同步Android设备时间