1,相信我们写过自定义控件的同学都会有一个疑问,自定义属性到底是怎么工作的,为什么要使用自定义属性呢,接下来结带着大家一起来学习学习,在学习这一篇的时候,可以下看看我的上一篇《从源码的角度一步步打造自己的TextView》,会对这篇有更好的理解
2,为什么要使用自定义属性
我们要使用自定义属性的话首先要我们有这个自定义属性,那么我们常见的控件Textview的Android:text属性是怎么来的呢,我们来一起看一下系统的自定义属性源码,系统定义的所有属性我们可以在\sdk\platforms\Android-xx\data\res\values目录下找到attrs.xml这个文件,这里只找几个很常见的view属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<declare-styleable name=
"View"
>
<attr name=
"id"
format=
"reference"
/>
<attr name=
"background"
format=
"reference|color"
/>
<attr name=
"padding"
format=
"dimension"
/>
...
<attr name=
"focusable"
format=
"boolean"
/>
...
</declare-styleable>
<declare-styleable name=
"TextView"
>
<attr name=
"text"
format=
"string"
localization=
"suggested"
/>
<attr name=
"hint"
format=
"string"
/>
<attr name=
"textColor"
/>
<attr name=
"textColorHighlight"
/>
<attr name=
"textColorHint"
/>
...
</declare-styleable>
<declare-styleable name=
"ViewGroup_Layout"
>
<attr name=
"layout_width"
format=
"dimension"
>
<
enum
name=
"fill_parent"
value=
"-1"
/>
<
enum
name=
"match_parent"
value=
"-1"
/>
<
enum
name=
"wrap_content"
value=
"-2"
/>
</attr>
<attr name=
"layout_height"
format=
"dimension"
>
<
enum
name=
"fill_parent"
value=
"-1"
/>
<
enum
name=
"match_parent"
value=
"-1"
/>
<
enum
name=
"wrap_content"
value=
"-2"
/>
</attr>
</declare-styleable>
<declare-styleable name=
"LinearLayout_Layout"
>
<attr name=
"layout_width"
/>
<attr name=
"layout_height"
/>
<attr name=
"layout_weight"
format=
"float"
/>
<attr name=
"layout_gravity"
/>
</declare-styleable>
<declare-styleable name=
"RelativeLayout_Layout"
>
<attr name=
"layout_centerInParent"
format=
"boolean"
/>
<attr name=
"layout_centerHorizontal"
format=
"boolean"
/>
<attr name=
"layout_centerVertical"
format=
"boolean"
/>
...
</declare-styleable>
|
首先我们知道我们所有的控件都是继承自view这个类的,所以view类所拥有的属性我们继承它的子类是全部都拥有的,而我们父view类却不能使用子view的特有的属性,充分的体现了我们的语言的多态性。再看看我们上面的标签,都有一个共同点,就是<declare-styleable name = "xxxx"> ,然后里面还有一堆的子标签,而这些子标签就表示这是这个XXX类的属性。但是并不是每个控件都能使用所有属性,LinearLayout中能使用layout_weight属性,而RelativeLayout却不能使用,因为layout_weight是为LinearLayout的LayoutParams定义的。
3,怎么自定义属性
我们根据上面的源码我们知道,这两种的区别就是attr标签后面带不带format属性,如果带format的就是在定义属性,如果不带format的就是在使用已有的属性,name的值就是属性的名字,format是限定当前定义的属性能接受什么值。而系统定义的属性一般引用都用android:XXX引用,如果我们现在要定义一个text属性,所以我们可以有两种定义我们的方式
常规方式:
1
2
3
4
5
|
<resources>
<declare-styleable name=
"MyTextView"
>
<attr name=“text
" format="
string" />
</declare-styleable>
</resources>
|
引用系统已经定义好的:
1
2
3
4
5
|
<resources>
<declare-styleable name=
"MyTextView"
>
<attr name=“android:text"/>
</declare-styleable>
</resources>
|
为什么这里我们还要引自系统属性呢,因为我们的MyTextView是继承的View,而android:text是TextView的特殊属性,所以这里必须要引用一下。
3,属性值的类型format
format一共支持11种类型
① reference
- 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable>
- 属性使用:
<ImageView android:background = "@drawable/图片ID"/>
② color
- 属性定义:
<attr name = "textColor" format = "color" />
- 属性使用:
<TextView android:textColor = "#00FF00" />
③boolean
- 属性定义:
<attr name = "focusable" format = "boolean" />
- 属性使用:
<Button android:focusable = "true"/>
④dimension
- 属性定义:
<attr name = "layout_width" format = "dimension" />
- 属性使用:
<Button android:layout_width = "42dip"/>
⑤float
- 属性定义:
<attr name = "fromAlpha" format = "float" />
- 属性使用:
1
|
<alpha android:fromAlpha =
"1.0"
/
|
⑥integer
- 属性定义:
<attr name = "framesCount" format="integer" />
- 属性使用:
<animated-rotate android:framesCount = "12"/>
⑦string
- 属性定义:
<attr name = "text" format = "string" />
- 属性使用:
<TextView android:text = "我是文本"/>
⑧fraction
- 属性定义:
<attr name = "pivotX" format = "fraction" />
- 属性使用:
<rotate android:pivotX = "200%"/>
⑨ enum:枚举值
- 属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
- 属性使用:
<LinearLayout android:orientation = "vertical"> </LinearLayout>
注意:当使用枚举属性的话不能在一个属性中同时使用两个值
⑩ flag:位或运算
- 属性定义:
<declare-styleable name="名称"> <attr name="gravity"> <flag name="top" value="0x30" /> <flag name="bottom" value="0x50" /> <flag name="left" value="0x03" /> <flag name="right" value="0x05" /> <flag name="center_vertical" value="0x10" /> ... </attr> </declare-styleable>
- 属性使用:
<TextView android:gravity="bottom|left"/>
位运算符在使用的时候可以使用多个属性
⑪混合类型:属性定义时可以指定多种类型值
- 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable>
- 属性使用:
<ImageView android:background = "@drawable/图片ID" /> 或者: <ImageView android:background = "#00FF00" />
以上就是所有的自定义属性的格式了,知道了这些,以后方便我们更准确的去定义自己的属性
4,在类中获取对应的自定义属性
首先在attrs文件中添加我们的自定义属性
1
2
3
4
5
6
7
8
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<resources>
<declare-styleable name=
"MyTextView2"
>
<attr name=
"myText"
format=
"string"
/>
<attr name=
"myTextColor"
format=
"color"
/>
<attr name=
"myTextSize"
format=
"dimension"
/>
</declare-styleable>
</resources>
|
然后我们在布局文件中添加我们的自定义属性,这里要注意一下要引入我们的自定义空间,一般来说有两种:xmlns:mytextview="http://schemas.android.com/apk/res-auto”,res-auto表示自动查找,还有一种写法xmlns:mytextview="http://schemas.android.com/apk/com.example.myview",com.example.myview 为我们的应用程序包名。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:myview=
"http://schemas.android.com/apk/res-auto"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
><br>
<com.qianmo.activitydetail.MyTextView2
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:background=
"#00ff00"
myview:myText=
" I Love You ......I Love You ......"
myview:myTextColor=
"#ff3399"
myview:myTextSize=
"25sp"
/>
</LinearLayout>
|
在构造方法中获取我们的自定义属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
MyTextView2(Context context,
@Nullable
AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
/**
* 初始化数据
*/
private
void
init(Context context, AttributeSet attrs,
int
defStyleAttr) {
//获取自定义属性的值
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView2, defStyleAttr,
0
);
mText = a.getString(R.styleable.MyTextView2_myText);
mTextColor = a.getColor(R.styleable.MyTextView2_myTextColor, Color.BLACK);
mTextSize = a.getDimension(R.styleable.MyTextView2_myTextSize, 30f);
a.recycle();
//初始化Paint数据
mPaint =
new
Paint();
mPaint.setColor(mTextColor);
mPaint.setTextSize(mTextSize);
//获取绘制的宽高
mBound =
new
Rect();
mPaint.getTextBounds(mText,
0
, mText.length(), mBound);
Log.i(TAG,
"mText :"
+ mText +
",mTextColor:"
+ mTextColor+
",mTextSize:"
+ mTextSize);
}
|
打印结果如下:
1
|
mText : I Love You ......I Love You ......,mTextColor:-
52327
,mTextSize:
65.625
|
ok,这样我们的就本上使用上了自定义属性,但是在使用的时候我们发现了两个陌生的类AttributeSet和TypeArray类,下面让我们来了解了解吧
5,AttributeSet和TypeArray的了解
Attributeset看名字就知道是一个属性的集合,实际上,它内部就是一个XML解析器,帮我们将布局文件中该控件的所有属性解析出来,并以key-value的兼职对形式维护起来。其实我们完全可以只用他通过下面的代码来获取我们的属性就行。
1
2
3
4
|
//打印AttributeSet中的值
for
(
int
i =
0
; i < attrs.getAttributeCount(); i++) {
Log.i(TAG,
"name:"
+ attrs.getAttributeName(i) +
",value:"
+ attrs.getAttributeValue(i));
}
|
看看我们打印出来的结果
1
2
3
4
5
6
7
|
03
-
21
03
:
24
:
44.339
12206
-
12206
/com.qianmo.activitydetail I/MyTextView: getWidth():
1080
,getHeight():
1731
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:background,value:#ff00ff00
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:layout_width,value:-
1
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:layout_height,value:-
1
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:myText,value: I Love You ......I Love You ......
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:myTextColor,value:#ffff3399
03
-
21
03
:
30
:
59.950
17617
-
17617
/com.qianmo.activitydetail I/MyTextView: name:myTextSize,value:
25
.0sp
|
可以看到使用Attributeset得到的属性的值是取到的xml文件中的值,而我们想要的textsize的大小,还得想方法将sp去掉才能拿到我们的25.0,这时候我们就需要一个人帮我们来这样做了,而typeArray就顺势而生了,我们来看看使用TypeArray得到的数据
1
2
3
4
5
6
|
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView2, defStyleAttr,
0
);
mText = a.getString(R.styleable.MyTextView2_myText);
mTextColor = a.getColor(R.styleable.MyTextView2_myTextColor, Color.BLACK);
mTextSize = a.getDimension(R.styleable.MyTextView2_myTextSize, 30f);
a.recycle();
Log.i(TAG,
"mText :"
+ mText +
",mTextColor:"
+ mTextColor +
",mTextSize:"
+ mTextSize);
|
打印的结果
1
|
mText : I Love You ......I Love You ......,mTextColor:-
52327
,mTextSize:
65.625
|
这样的数据就是我们想要的了,这样我们就了解的差不多了,See You Next Time !