Android 自定义属性

本文以 ToggleButton 为例,介绍 Android 自定义属性的基本流程,代码在:https://github.com/qiracle/ToggleButton

定义 attrs.xml

在values目录下增加 attrs.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<resources>


<!-- 声名属性集的名称 -->
<declare-styleable name="MyToggleBtn">

<!-- 声名一个属性 name是my_background 类型为 引用类型 引用资源ID -->
<attr name="my_background" format="reference" />

<!-- 声名一个属性 name是my_slide_btn 类型为 引用类型 引用资源ID -->
<attr name="my_slide_btn" format="reference" />

<!-- 声名一个属性 name是curr_state 类型为 boolean 类型-->
<attr name="curr_state" format="boolean" />

</declare-styleable>


</resources>

在布局文件中引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<!--命名空间 qiracle ,给下面使用-->
xmlns:qiracle="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<cn.qiracle.customattr.MyToggleButton
android:id="@+id/my_toggle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
qiracle:my_background="@drawable/switch_background"
qiracle:my_slide_btn="@drawable/slide_button"
qiracle:curr_state="false"
testAttrs="hello"
/>

</RelativeLayout>

在代码里设置属性

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 在布局文件中声名的view,创建时由系统自动调用。
* @param context 上下文对象
* @param attrs 属性集
*/

public MyToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);


//无命名空间测试

String testAttrs = attrs.getAttributeValue(null, "testAttrs");

System.out.println("testAttrs===:"+testAttrs);

/*
* AttributeSet 对XML文件解析后的结果,封装为 AttributeSet 对象。
* 存储的都是原始数据。仅对数据进行简单加工。
*/


int count = attrs.getAttributeCount();
// 打印MyToggleButton所有属性 ,也就是上面布局文件里的9个
for (int i = 0; i < count; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
// System.out.println("name : "+name+" value : "+value);
}


//获得自定义的属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);

int N = ta.getIndexCount();
for (int i = 0; i < N; i++) {
/*
* 获得某个属性的ID值
*/

int itemId = ta.getIndex(i);
switch (itemId) {
case R.styleable.MyToggleBtn_curr_state:
currState = ta.getBoolean(itemId, false);

break;
case R.styleable.MyToggleBtn_my_background:
backgroundId = ta.getResourceId(itemId, -1);
if(backgroundId == -1){
throw new RuntimeException("请设置背景图片");
}
backgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundId);

break;
case R.styleable.MyToggleBtn_my_slide_btn:
slideBtnId = ta.getResourceId(itemId, -1);
slideBtn = BitmapFactory.decodeResource(getResources(), slideBtnId);

break;

default:
break;
}

}
ta.recycle(); //回收ta,不回收as会提示

initView();
}
文章目录
  1. 1. 定义 attrs.xml
  2. 2. 在布局文件中引用
  3. 3. 在代码里设置属性
|