读取layout属性-AttributeSet TypedArray
2016-10-31 17:13
127 查看
假如说要读取layout_height属性
两种方式,第一种
layout_height=“match_parent”
viewHeight 取到的值为 “match_parent”
layout_height=“100dip”
viewHeight 取到的值为 “100dip”
如果用 getAttributeIntValue 读取的话, 无法正确读取到数值
很显然这种方式读取数值不是很爽
第二种方式: 通过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-2
try {
// 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) {// 0x10
initViewHeight = (int) typedArray.getDimension(1, ViewGroup.LayoutParams.MATCH_PARENT);
initViewHeight = TypedValue.complexToDimensionPixelSize(typeValue.data, getResources().getDisplayMetrics());
} else if (typeValue.type == TypedValue.TYPE_FIRST_INT) {// 0x05
initViewHeight = typeValue.data;
}
} catch (Exception e) {
// java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10
e.printStackTrace();
}
typedArray.recycle();
两种方式,第一种
/** * 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-2
try {
// 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) {// 0x10
initViewHeight = (int) typedArray.getDimension(1, ViewGroup.LayoutParams.MATCH_PARENT);
initViewHeight = TypedValue.complexToDimensionPixelSize(typeValue.data, getResources().getDisplayMetrics());
} else if (typeValue.type == TypedValue.TYPE_FIRST_INT) {// 0x05
initViewHeight = typeValue.data;
}
} catch (Exception e) {
// java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10
e.printStackTrace();
}
typedArray.recycle();
相关文章推荐
- 读取属性文件
- android:layout_gravity和android:gravity属性的区别
- Android RelativeLayout 属性
- linearLayout 和 relativeLayout的属性区别
- Android layout属性大全
- [Unix]shell编程珠玑(4)-- 读取属性文件和访问CVS
- 代码控制控件的layout_weight属性和结合xml填充屏幕
- 淘宝API读取商品属性
- android:gravity / android:layout_Gravity,android:padding / android:layout_margin属性区分
- LinearLayout中layout_weight属性
- edgesForExtendLayout 属性
- RelativeLayout用到的一些重要的属性
- RelativeLayout中的位置属性
- vb 读取文件属性
- spring中属性文件properties的读取
- 读取文件属性 FileGetAttr
- 单例读取属性文件
- 用ASP读取EXCHANGE2000中的邮件属性-已读或未读
- LinearLayout和RelativeLayout 属性对比
- layout属性