写一个条目
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="65dp" >
<TextView
android:id="@+id/tv_setting_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="自动更新应用"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_setting_title1"
android:layout_marginLeft="15dp"
android:text="自动更新已经关闭"
android:textColor="#88000000"
android:textSize="14sp" />
<CheckBox
android:id="@+id/setting_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false" 牺牲checkbox的点击 成全整个控件的点击
android:layout_marginRight="10dp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#000000" />
</RelativeLayout>
然后自定义一个控件 SettingItemView
package com.zbzbhahae.mobilesafe.ui;
import com.zbzbhahae.mobilesafe.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 自定义组合控件 有两个TextView 还有一个CheckBox 还有一个View
* @author zbzbhahae
*
*/
public class SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_desc;
private TextView tv_title;
private String desc_on;
private String desc_off;
/**
* 初始化布局文件
* @param context
*/
private void initView(Context context) {
//布局文件转换成View 并且加载到SettingItemView中
View.inflate(context, R.layout.item_setting_list, SettingItemView.this);
cb_status = (CheckBox) this.findViewById(R.id.setting_cb);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) this.findViewById(R.id.tv_setting_title1);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
String thetitle = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zbzbhahae.mobilesafe", "thetitle");//attrs中写的3个属性
desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zbzbhahae.mobilesafe", "desc_on");
desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zbzbhahae.mobilesafe", "desc_off");
tv_title.setText(thetitle);
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
/**
* 校验组合空间是否选中
*/
public boolean isChecked() {
return cb_status.isChecked();
}
/**
* 设置组合控件的是否选中
*/
public void setChecked(boolean checked) {
if(checked) {
setDesc(desc_on);
} else {
setDesc(desc_off);
}
cb_status.setChecked(checked);
}
/**
* 设置组合控件的描述信息
*/
public void setDesc(String text) {
tv_desc.setText(text);
}
}
设置界面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zbzbhahae="http://schemas.android.com/apk/res/com.zbzbhahae.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<TextView
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="设置中心"
android:gravity="center"
android:background="#8866FF00"
android:textColor="#000000"
android:textSize="30sp"
android:layout_gravity="center"/>
<com.zbzbhahae.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
zbzbhahae:thetitle="设置自动更新"
zbzbhahae:desc_on="自动更新已经开启"
zbzbhahae:desc_off="自动更新已经关闭"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
zbzbhahae:thetitle="设置自动更新"
zbzbhahae:desc_on="自动更新已经开启"
zbzbhahae:desc_off="自动更新已经关闭"
上面3条需要在values/attrs中定义 线性布局中要添加命名空间
xmlns:zbzbhahae="http://schemas.android.com/apk/res/com.zbzbhahae.mobilesafe"
后面是自己应用的包名
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="thetitle" format="string"/>
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>