Class Overview
Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int)
or obtainAttributes(AttributeSet, int[])
. Be sure to call recycle()
when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
解析:可以通过obtainStyledAttributes(AttributeSet, int[], int, int)
or obtainAttributes(AttributeSet, int[])这两个方法得到
用完需要使用recycle()
释放。
getDimensionPixelSize(int index, int defValue)
Retrieve a dimensional unit attribute at
index for use as a size in raw pixels.
例1:
TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView); |
28 | float textSize = a.getDimension(R.styleable.myView_textSize, 30 ); |
29 | int textColor = a.getColor(R.styleable.myView_ textColor, 0xFFFFFFFF ); |
2. [代码]attrs.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
3 | < declare-styleable name = "myView" > |
4 | < attr name = "textColor" format = "color" /> |
5 | < attr name = "textSize" format = "dimension" /> |
例2:
TypedArray a=context.obtainStyledAttributes
(attrs,R.styleable.
com_deezapps_widget_HorizontalPager
);
//TypedArray是一个数组容器
pageWidthSpec=a.getDimensionPixelSize
(R.styleable.com_deezapps_widget_HorizontalPager
_
pageWidth, SPEC_UNDEFINED);
//防止在XML文件里没有定义,就加上了默认值SPEC_UNDEFINED
a.recycle();
attrs.xml里:
<declare-styleable name="
com.deezapps.widget.HorizontalPager
">
<attr name="
pageWidth
" format="dimension" />
</declare-styleable>
3. [代码]main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | xmlns:test = "http://schemas.android.com/apk/res/com.adnroid.test" |
04 | android:orientation = "vertical" |
05 | android:layout_width = "fill_parent" |
06 | android:layout_height = "fill_parent" |
09 | android:layout_width = "fill_parent" |
10 | android:layout_height = "wrap_content" |
11 | android:text = "@string/hello" |
13 | < com.adnroid.test.MyView |
14 | android:layout_width = "fill_parent" |
15 | android:layout_height = "fill_parent" |
<com.deezapps.widget.HorizontalPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
deezapps:pageWidth="250dip"
>