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

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="5dp" > <TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="22sp" /> <TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginTop="3dp"
android:textColor="@color/gray"
android:textSize="18sp" /> <CheckBox
android:id="@+id/cb_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" /> <View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:layout_alignParentBottom="true"
android:background="@color/gray" /> </RelativeLayout>

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

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; public class SettingItemView extends RelativeLayout { public SettingItemView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initview();
} public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview();
} public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initview();
} public SettingItemView(Context context) {
super(context);
initview();
} private void initview() {
View.inflate(getContext(), R.layout.setting_item_view, this); }

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

 <?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="update_on" format="string" />
<attr name="update_off" format="string" />
</declare-styleable> </resources>

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

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; public class SettingItemView extends RelativeLayout {
// 命名空间,与布局文件中写的命名空间保持一致
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.haha.mobilesafe";
private TextView tv_title;
private TextView tv_desc;
private CheckBox cb_status;
private String mTitle;
private String mUpdateOn;
private String mUpdateOff; public SettingItemView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initview();
} public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview();
} public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
mTitle = attrs.getAttributeValue(NAMESPACE, "title");
mUpdateOn = attrs.getAttributeValue(NAMESPACE, "update_on");
mUpdateOff = attrs.getAttributeValue(NAMESPACE, "update_off"); initview();
} public SettingItemView(Context context) {
super(context);
initview();
} private void initview() {
View.inflate(getContext(), R.layout.setting_item_view, this);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_desc = (TextView) findViewById(R.id.tv_desc);
cb_status = (CheckBox) findViewById(R.id.cb_status);
setTitle(mTitle);
} // 设置item的名称
public void setTitle(String text) {
tv_title.setText(text);
} // 设置item的描述
public void setDesc(String text) {
tv_desc.setText(text);
} // 获取checkbox是否勾选
public boolean isChecked() {
return cb_status.isChecked();
} // 设置checkbox勾选状态
public void setChecked(boolean check) {
if (check) {
setDesc(mUpdateOn);
} else {
setDesc(mUpdateOff);
}
cb_status.setChecked(check);
} }

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

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:haha="http://schemas.android.com/apk/res/com.haha.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
style="@style/titlebar"
android:text="设置中心" /> <com.haha.mobilesafe.view.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
haha:title="自动更新设置"
haha:update_off="自动更新已关闭"
haha:update_on="自动更新已开启" /> </LinearLayout>
上一篇:rail server 启动时报告错误undefine mysql_get_client_info


下一篇:MySQL逻辑模块工作配合