TypedArray和attrs.xml和AttributeSet这一系列都是自定义控件属性时要用到的内容。
首先说说attrs.xml:它是定义成类似于这种形式的。
- @H_502_9@<?xmlversion="1.0"encoding="utf-8"@H_502_9@?>
- @H_502_9@<resources@H_502_9@>
- @H_502_9@<declare-styleablename="MyView"@H_502_9@>
- @H_502_9@<attrname="textColor"format="color"@H_502_9@>@H_502_9@</attr@H_502_9@>
- @H_502_9@<attrname="textSize"format="dimension"@H_502_9@/>
- @H_502_9@</declare-styleable@H_502_9@>
- @H_502_9@</resources@H_502_9@>
1.主要讲讲里面的format属性:
①. reference:参考某一资源ID。
(1)属性定义:
(2)属性使用:
②. color:颜色值。
(1)属性定义:
(2)属性使用:
③. boolean:布尔值。
(1)属性定义:
(2)属性使用:
④. dimension:尺寸值。
(1)属性定义:
(2)属性使用:
⑤. float:浮点值。
(1)属性定义:
(2)属性使用:
⑥. integer:整型值。
(1)属性定义:
- @H_502_9@<declare-styleablename="AnimatedRotateDrawable"@H_502_9@>
- @H_502_9@<attrname="visible"@H_502_9@/>
- @H_502_9@<attrname="frameDuration"format="integer"@H_502_9@/>
- @H_502_9@<attrname="framesCount"format="integer"@H_502_9@/>
- @H_502_9@<attrname="pivotX"@H_502_9@/>
- @H_502_9@<attrname="pivotY"@H_502_9@/>
- @H_502_9@<attrname="drawable"@H_502_9@/>
- @H_502_9@</declare-styleable@H_502_9@>
(2)属性使用:
⑦. string:字符串。
(1)属性定义:
(2)属性使用:
⑧. fraction:百分数。
(1)属性定义:
- @H_502_9@<declare-styleablename="RotateDrawable"@H_502_9@>
- @H_502_9@<attrname="visible"@H_502_9@/>
- @H_502_9@<attrname="fromDegrees"format="float"@H_502_9@/>
- @H_502_9@<attrname="toDegrees"format="float"@H_502_9@/>
- @H_502_9@<attrname="pivotX"format="fraction"@H_502_9@/>
- @H_502_9@<attrname="pivotY"format="fraction"@H_502_9@/>
- @H_502_9@<attrname="drawable"@H_502_9@/>
- @H_502_9@</declare-styleable@H_502_9@>
(2)属性使用:
⑨. enum:枚举值。
(1)属性定义:
(2)属性使用:
10. flag:位或运算。
(1)属性定义:
- @H_502_9@<declare-styleablename="名称"@H_502_9@>
- @H_502_9@<attrname="windowSoftInputMode"@H_502_9@>
- @H_502_9@<flagname="stateUnspecified"value="0"@H_502_9@/>
- @H_502_9@<flagname="stateUnchanged"value="1"@H_502_9@/>
- @H_502_9@<flagname="stateHidden"value="2"@H_502_9@/>
- @H_502_9@<flagname="stateAlwaysHidden"value="3"@H_502_9@/>
- @H_502_9@<flagname="stateVisible"value="4"@H_502_9@/>
- @H_502_9@<flagname="stateAlwaysVisible"value="5"@H_502_9@/>
- @H_502_9@<flagname="adjustUnspecified"value="0x00"@H_502_9@/>
- @H_502_9@<flagname="adjustResize"value="0x10"@H_502_9@/>
- @H_502_9@<flagname="adjustPan"value="0x20"@H_502_9@/>
- @H_502_9@<flagname="adjustNothing"value="0x30"@H_502_9@/>
- @H_502_9@</attr@H_502_9@>
- @H_502_9@</declare-styleable@H_502_9@>
- @H_502_9@<activity
- android:name=".StyleAndThemeActivity"
- android:label="@string/app_name"
- android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden"@H_502_9@>
- @H_502_9@<intent-filter@H_502_9@>
- @H_502_9@<actionandroid:name="android.intent.action.MAIN"@H_502_9@/>
- @H_502_9@<categoryandroid:name="android.intent.category.LAUNCHER"@H_502_9@/>
- @H_502_9@</intent-filter@H_502_9@>
- @H_502_9@</activity@H_502_9@>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
(2)属性使用:
下面是一个布局文件:
- @H_502_9@<?xml
- version="1.0"encoding="utf-8"@H_502_9@?>
- @H_502_9@<LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- //自定义的View的路径为com.xc.demo
- xmlns:test="http://schemas.android.com/apk/res/com.xc.demo"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- @H_502_9@>
- @H_502_9@<TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- @H_502_9@/>
- @H_502_9@<com.xc.demo.MyView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- //这里引用改变字体和颜色 textColor和textSize都是根据前面的attrs.xml文件设置的
- test:textSize="100dp"
- test:textColor="#ff0000"
- @H_502_9@/>
- @H_502_9@</LinearLayout@H_502_9@>
2.TypedArray的作用是在代码中设置。
- publicMyView(Contextcontext,AttributeSetattrs){
- this(context);
- TypedArraya=context
- .obtainStyledAttributes(attrs,R.styleable.MyView);
- intcolor=a.getColor(R.styleable.MyView_textColor,0XFF0000FF);
- floatsize=a.getDimension(R.styleable.MyView_textSize,50);
- mPaint.setColor(color);
- mPaint.setTextSize(size);
- a.recycle();
- }
R.styleable.MyView是attrs.xml中<declare-styleable name="MyView">的名字
具体的设置是依靠名字+"_"+名字得到来设置的。
3.如果代码和xml中都设置了,一般以xml中设置为先。
本文出自 “千寻” 博客,请务必保留此出处http://www.jb51.cc/article/p-gpxgtuvz-ue.html