Android开发之自定义组合控件

自定义组合控件的步骤
1.自定义一个View,继承ViewGroup,比如RelativeLayout
2.编写组合控件的布局文件,在自定义的view中加载
(使用View.inflate())
3.自定义属性,
在value中创建一个attrs.xml文件,定义自己的属性
4.在自定义View的java代码中完成逻辑
5.在使用自定义view的布局文件中,添加自己的命名空间,在自定义view中,手动添加自己定义的属性

如:

1.自定义一个View,继承ViewGroup。命名为:setting_item_view.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="70dp"
 5     android:padding="5dp" >
 6 
 7     <TextView
 8         android:id="@+id/tv_title"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:textColor="@color/black"
12         android:textSize="22sp" />
13 
14     <TextView
15         android:id="@+id/tv_desc"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_below="@id/tv_title"
19         android:layout_marginTop="3dp"
20         android:textColor="@color/gray"
21         android:textSize="18sp" />
22 
23     <CheckBox
24         android:id="@+id/cb_status"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_alignParentRight="true"
28         android:layout_centerVertical="true"
29         android:clickable="false"
30         android:focusable="false"
31         android:focusableInTouchMode="false" />
32 
33     <View
34         android:layout_width="match_parent"
35         android:layout_height="0.3dp"
36         android:layout_alignParentBottom="true"
37         android:background="@color/gray" />
38 
39 </RelativeLayout>

2.编写组合控件的布局文件,在自定义的view中加载。命名:SettingItemView.java

 1 import android.content.Context;
 2 import android.util.AttributeSet;
 3 import android.view.View;
 4 import android.widget.CheckBox;
 5 import android.widget.RelativeLayout;
 6 import android.widget.TextView;
 7 
 8 
 9 public class SettingItemView extends RelativeLayout {
10 
11     public SettingItemView(Context context, AttributeSet attrs,
12             int defStyleAttr, int defStyleRes) {
13         super(context, attrs, defStyleAttr, defStyleRes);
14         initview();
15     }
16 
17     public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
18         super(context, attrs, defStyleAttr);
19         initview();
20     }
21 
22     public SettingItemView(Context context, AttributeSet attrs) {
23         super(context, attrs);
24         initview();
25     }
26 
27     public SettingItemView(Context context) {
28         super(context);
29         initview();
30     }
31 
32     private void initview() {
33         View.inflate(getContext(), R.layout.setting_item_view, this);
34 
35     }

3.在value中创建一个attrs.xml文件,定义自己的属性,title, update_on, update_off

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <declare-styleable name="SettingItemView">
 5         <attr name="title" format="string" />
 6         <attr name="update_on" format="string" />
 7         <attr name="update_off" format="string" />
 8     </declare-styleable>
 9 
10 </resources>

4.在自定义View的java代码中完成逻辑,SettingItemView.java

 1 import android.content.Context;
 2 import android.util.AttributeSet;
 3 import android.view.View;
 4 import android.widget.CheckBox;
 5 import android.widget.RelativeLayout;
 6 import android.widget.TextView;
 7 
 8 
 9 public class SettingItemView extends RelativeLayout {
10     // 命名空间,与布局文件中写的命名空间保持一致
11     private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.haha.mobilesafe";
12     private TextView tv_title;
13     private TextView tv_desc;
14     private CheckBox cb_status;
15     private String mTitle;
16     private String mUpdateOn;
17     private String mUpdateOff;
18 
19     public SettingItemView(Context context, AttributeSet attrs,
20             int defStyleAttr, int defStyleRes) {
21         super(context, attrs, defStyleAttr, defStyleRes);
22         initview();
23     }
24 
25     public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
26         super(context, attrs, defStyleAttr);
27         initview();
28     }
29 
30     public SettingItemView(Context context, AttributeSet attrs) {
31         super(context, attrs);
32         mTitle = attrs.getAttributeValue(NAMESPACE, "title");
33         mUpdateOn = attrs.getAttributeValue(NAMESPACE, "update_on");
34         mUpdateOff = attrs.getAttributeValue(NAMESPACE, "update_off");
35 
36         initview();
37     }
38 
39     public SettingItemView(Context context) {
40         super(context);
41         initview();
42     }
43 
44     private void initview() {
45         View.inflate(getContext(), R.layout.setting_item_view, this);
46         tv_title = (TextView) findViewById(R.id.tv_title);
47         tv_desc = (TextView) findViewById(R.id.tv_desc);
48         cb_status = (CheckBox) findViewById(R.id.cb_status);
49         setTitle(mTitle);
50     }
51 
52     // 设置item的名称
53     public void setTitle(String text) {
54         tv_title.setText(text);
55     }
56 
57     // 设置item的描述
58     public void setDesc(String text) {
59         tv_desc.setText(text);
60     }
61 
62     // 获取checkbox是否勾选
63     public boolean isChecked() {
64         return cb_status.isChecked();
65     }
66 
67     // 设置checkbox勾选状态
68     public void setChecked(boolean check) {
69         if (check) {
70             setDesc(mUpdateOn);
71         } else {
72             setDesc(mUpdateOff);
73         }
74         cb_status.setChecked(check);
75     }
76 
77 }

5.在使用自定义view的布局文件中,添加自己的命名空间,在自定义view中,手动添加自定义的属性

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:haha="http://schemas.android.com/apk/res/com.haha.mobilesafe"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         style="@style/titlebar"
10         android:text="设置中心" />
11 
12     <com.haha.mobilesafe.view.SettingItemView
13         android:id="@+id/siv_update"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         haha:title="自动更新设置"
17         haha:update_off="自动更新已关闭"
18         haha:update_on="自动更新已开启" />
19 
20 </LinearLayout>
原文地址:https://www.cnblogs.com/liyiran/p/5127196.html