android自定义组合控件

简介

组合控件就是把已有的控件组合组合,作为一个新的控件让系统使用
组合控件样例
上图就是左右两个button和中间一个textView组成,他可以被当成一个控件向外面提供一些属性和方法

实现

实现步骤

  1. 创建属性表
  2. 创建layout布局
  3. 继承一个viewgroup并加载布局
  4. 调用

创建属性表

在value文件夹下创建一个attrs.xml
新建一些属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<declare-styleable name="MyTitleBar">
<attr name="title_background_color" format="reference|integer" />
<attr name="left_button_visible" format="boolean" />
<attr name="right_button_visible" format="boolean" />
<attr name="title_text" format="string" />
<attr name="title_text_color" format="color" />
<attr name="title_text_drawable" format="reference|integer" />
<attr name="right_button_text" format="string" />
<attr name="right_button_text_color" format="color" />
<attr name="right_button_drawable" format="reference|integer" />
<attr name="left_button_text" format="string" />
<attr name="left_button_text_color" format="color" />
<attr name="left_button_drawable" format="reference|integer" />
</declare-styleable>

比如第一个属性,他是创建要给叫title_background_color的属性,属性的数据类型是reference或者integer类型的,具体都有什么类型的属性参考这里

创建一个layout布局

在layout创建一个my_view.xml布局文件

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
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minWidth="45dp"
android:minHeight="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />

<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minWidth="45dp"
android:minHeight="45dp"
android:textSize="14sp" />
</merge>

里面写了上面说的包含的三个已有控件,这个自定义组合控件是继承RelativeLayout,但是布局中的根标签用的是merge,是因为一会java中加载控件的时候相当于外面会加一层RelativeLayout,所以这里根布局不用加但是里面控件的相对位置按照RelativeLayout来写就行了

继承一个viewgroup并加载布局

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class MyTitleBar extends RelativeLayout {
private static final String TAG = "MyTitleBarr";
private Button titleBarLeftBtn;
private Button titleBarRightBtn;
private TextView titleBarTitle;

public MyTitleBar(Context context) {
this(context,null);
}
public MyTitleBar(final Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.my_view, this, true);
titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
titleBarTitle = (TextView) findViewById(R.id.title_bar_title);

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MyTitleBar);
if (attributes != null) {
//处理titleBar背景色
int titleBarBackGround = attributes.getResourceId(R.styleable.MyTitleBar_title_background_color, R.color.colorPrimary);
setBackgroundResource(titleBarBackGround);
//先处理左边按钮
//获取是否要显示左边按钮
boolean leftButtonVisible = attributes.getBoolean(R.styleable.MyTitleBar_left_button_visible, true);
if (leftButtonVisible) {
titleBarLeftBtn.setVisibility(View.VISIBLE);
} else {
titleBarLeftBtn.setVisibility(View.INVISIBLE);
}
//设置左边按钮的文字
String leftButtonText = attributes.getString(R.styleable.MyTitleBar_left_button_text);
if (!TextUtils.isEmpty(leftButtonText)) {
titleBarLeftBtn.setText(leftButtonText);
//设置左边按钮文字颜色
int leftButtonTextColor = attributes.getColor(R.styleable.MyTitleBar_left_button_text_color, Color.WHITE);
titleBarLeftBtn.setTextColor(leftButtonTextColor);
}
//设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
int leftButtonDrawable = attributes.getResourceId(R.styleable.MyTitleBar_left_button_drawable, R.mipmap.ic_launcher);
if (leftButtonDrawable != -1) {
titleBarLeftBtn.setCompoundDrawablesWithIntrinsicBounds(leftButtonDrawable, 0, 0, 0); //设置到哪个控件的位置()
}


//处理标题
//先获取标题是否要显示图片icon
int titleTextDrawable = attributes.getResourceId(R.styleable.MyTitleBar_title_text_drawable,-1);
if (titleTextDrawable != -1) {
titleBarTitle.setBackgroundResource(titleTextDrawable);
} else {
//如果不是图片标题 则获取文字标题
String titleText = attributes.getString(R.styleable.MyTitleBar_title_text);
if (!TextUtils.isEmpty(titleText)) {
titleBarTitle.setText(titleText);
}
//获取标题显示颜色
int titleTextColor = attributes.getColor(R.styleable.MyTitleBar_title_text_color, Color.WHITE);
titleBarTitle.setTextColor(titleTextColor);
}

//先处理右边按钮
//获取是否要显示右边按钮
boolean rightButtonVisible = attributes.getBoolean(R.styleable.MyTitleBar_right_button_visible, true);
if (rightButtonVisible) {
titleBarRightBtn.setVisibility(View.VISIBLE);
} else {
titleBarRightBtn.setVisibility(View.INVISIBLE);
}
//设置右边按钮的文字
String rightButtonText = attributes.getString(R.styleable.MyTitleBar_right_button_text);
if (!TextUtils.isEmpty(rightButtonText)) {
titleBarRightBtn.setText(rightButtonText);
//设置右边按钮文字颜色
int rightButtonTextColor = attributes.getColor(R.styleable.MyTitleBar_right_button_text_color, Color.WHITE);
titleBarRightBtn.setTextColor(rightButtonTextColor);
}
//设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
int rightButtonDrawable = attributes.getResourceId(R.styleable.MyTitleBar_right_button_drawable, R.mipmap.ic_launcher);
if (rightButtonDrawable != -1) {
titleBarRightBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightButtonDrawable, 0); //设置到哪个控件的位置()
}
attributes.recycle();
}
}

public void setTitleClickListener(OnClickListener onClickListener) {
if (onClickListener != null) {
titleBarLeftBtn.setOnClickListener(onClickListener);
titleBarRightBtn.setOnClickListener(onClickListener);
}
}

public Button getTitleBarLeftBtn() {
return titleBarLeftBtn;
}

public Button getTitleBarRightBtn() {
return titleBarRightBtn;
}

public TextView getTitleBarTitle() {
return titleBarTitle;
}

}
  • 首先注意构造方法,有两个构造方法,只有一个参数的构造方法是在代码中动态new的时候调用的方法
    两个参数的构造方法是在布局中写的时候调用的构造方法,第二个参数attr就是在布局文件中写的属性集合

  • 控件定义的属性的时候先通过以下代码获取到属性集合

    1
    TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MyTitleBar);

然后通过以下方式获取到具体属性的值,如果没有定义该属性值,就会获得第二个参数的值

1
boolean leftButtonVisible = attributes.getBoolean(R.styleable.MyTitleBar_left_button_visible, true);

注意获得单个属性值的时候是R.styleable.(attrs中declare-styleable的名字)_(属性的名字)

调用

在布局中调用的时候要命名一个本地的APP空间,即可调用attrs的那些参数,也可以在类中多写一些方法能动态修改那些参数
activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<cn.xwmdream.myViews.MyTitleBar
android:layout_width="match_parent"
app:title_text="我是标题"
android:layout_height="wrap_content" />
</LinearLayout>