读取layout属性-AttributeSet TypedArray
来源:互联网 发布:linux gre over ipsec 编辑:程序博客网 时间:2024/05/29 19:08
假如说要读取layout_height属性
两种方式,第一种
/** * Returns the value of the specified attribute as a string representation. * * @param index Index of the desired attribute, 0...count-1. * * @return A String containing the value of the attribute, or null if the * attribute cannot be found. */ public String getAttributeValue(int index);String namespace = "http://schemas.android.com/apk/res/android";viewHeight = attrs.getAttributeIntValue(namespace, "layout_height", ViewGroup.LayoutParams.MATCH_PARENT);
layout_height=“match_parent”
viewHeight 取到的值为 “match_parent”
layout_height=“100dip”
viewHeight 取到的值为 “100dip”
如果用 getAttributeIntValue 读取的话, 无法正确读取到数值
/** * Return the integer value of 'attribute'. * * @param namespace Namespace of attribute to retrieve. * @param attribute The attribute to retrieve. * @param defaultValue What to return if the attribute isn't found. * * @return Resulting value. */ public int getAttributeIntValue(String namespace, String attribute, int defaultValue);
很显然这种方式读取数值不是很爽
第二种方式: 通过TypedArray读取
int[] arr = new int[] { android.R.attr.layout_width, android.R.attr.layout_height };TypedArray typedArray = context.obtainStyledAttributes(attrs, arr);// MATCH_PARENT -1 WRAP_CONTENT-2try {// String namespace = "http://schemas.android.com/apk/res/android";// initViewHeight = attrs.getAttributeIntValue(namespace, "layout_height", ViewGroup.LayoutParams.MATCH_PARENT);TypedValue typeValue = typedArray.peekValue(1);if (typeValue.type == TypedValue.TYPE_DIMENSION) {// 0x10initViewHeight = (int) typedArray.getDimension(1, ViewGroup.LayoutParams.MATCH_PARENT);initViewHeight = TypedValue.complexToDimensionPixelSize(typeValue.data, getResources().getDisplayMetrics());} else if (typeValue.type == TypedValue.TYPE_FIRST_INT) {// 0x05initViewHeight = typeValue.data;}} catch (Exception e) {// java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10e.printStackTrace();}typedArray.recycle();
0 0
- 读取layout属性-AttributeSet TypedArray
- 读取属性:TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs,...)
- 自定义控件属性TypedArray和attrs.xml和AttributeSet
- 深入理解android自定义属性(AttributeSet,TypedArray)
- TypedArray、attrs.xml、AttributeSet
- 初探TypedArray和AttributeSet
- attrs.xml AttributeSet TypedArray obtainStyledAttributes
- 菜鸟进阶之深入理解android自定义属性(AttributeSet,TypedArray)
- AttributeSet 帮助类,读取Android的属性---1
- AttributeSet 帮助类,读取Android的属性---2
- 资源访问机制之AttributeSet与TypedArray
- 自定义组件,AttributeSet和TypedArray的关系
- 自定义控件5---TypedArray和AttributeSet
- Android-自定义AttributeSet属性
- 自定义AttributeSet属性
- android 自定义AttributeSet属性
- 自定义AttributeSet属性
- android使用自定义属性AttributeSet
- 二分题目最全总结(持续更新)
- resource deadlock would occur
- Java初级程序员到CTO的技术路线图
- 如何定义BaseActivity与设定TitleBar
- 10.31
- 读取layout属性-AttributeSet TypedArray
- C#快速写延迟函数
- Java设计模式之——状态模式
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
- Android CoordinatorLayout与滚动的处理
- JSP开发中对jstl的引用方式(标签库引用)
- Jersey框架二:Jersey对JSON的支持
- JavaScript 传参
- iOS细节篇-block循环引用问题,和block安全使用方法