一.目的
1.做一个条目可以点击条目中任何一个位置都能选中checkbox
2.效果图如下:
二.方法步骤及代码
1.方法步骤
1.先写好自己想要做的组合控件的布局
2.1把布局加到自定义的控件里面并写好功能(比如这里的内容设置,和checkbox设置)
2.2 要把相关属性传到自定义的布局需要如果几步
(1). 参照系统源码attrs.xml, 找到定义TextView属性的位置,拷贝相关代码
Value/attrs.xml
(2) 创建attrs.xml, 定义相关属性
<!-- 自定义属性 -->
<declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
(3)在自定义组合控件里面获取相关属性,并设置给对应控件,然后就可以了
2.代码
(1)想要做成的组合空间的布局
setting_check_view.xml
<?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="wrap_content"
android:background="#00ff00"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="我是内容,请选择"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#00ff00"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
注意:如果不嵌套一个RelativeLayout.xml布局,那么CheckBox里面设置marginright会导致最外面设置背景(#00ff00),在margin的那一部分显示不出来,而且嵌套,设置的高度貌似会不生效(尽管预览效果是对的,实际跑起来就不对),还有就是可以通过设置Checkbox的BackGroud来避免他点击的时候有一圈阴影
错误(缺了margin那一块)
<?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="50dp"
android:background="#00ff00"
>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="我是内容,请选择"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#00ff00"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
错误2(设置的高度不生效)
<?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="50dp" <span style="white-space:pre"> </span>//注意这里!!!!!!!!!!!!!!!!!<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
android:background="#00ff00"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"> // 注意这里!!!!!!!!!!!!!!!!!!!!
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="我是内容,请选择"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#00ff00"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
(2).写到组合控件里面去
SettingCheckView.java
public class SettingCheckView extends LinearLayout{
private TextView tv_content;
private CheckBox cb_status;
public SettingCheckView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
<span style="white-space:pre"> </span>//这个<span style="font-family: Arial, Helvetica, sans-serif;">http://schemas.android.com/com.weixin.justtext1104要跟布局里的一样</span>
String bigtitle = attrs.getAttributeValue("http://schemas.android.com/com.weixin.justtext1104","bigtitle");
//从布局中获取文本,写给自己的ui
tv_content.setText(bigtitle);
}
private void initView(Context context) {
addView(View.inflate(context, R.layout.setting_check_view, null));
tv_content = (TextView) findViewById(R.id.tv_content);
cb_status = (CheckBox) findViewById(R.id.cb_checkbox);
}
/**
* 让文本选中
* */
public void setChecked(boolean state){
cb_status.setChecked(state);
}
/**
* 判断是否选中
* */
public boolean isChecked(){
return cb_status.isChecked();
}
}
(3).使用过组合控件的部分
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
//注意这个名字要attr.xml中的要一样(命名空间一般用包名就行) xmlns:SettingCheckView="http://schemas.android.com/com.weixin.justtext1104" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.weixin.justtext1104.SettingCheckView android:id="@+id/scv" android:layout_width="fill_parent" android:layout_height="wrap_content" SettingCheckView:bigtitle = "我是一个选项请勾选" > </com.weixin.justtext1104.SettingCheckView> </RelativeLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SettingCheckView"> <attr name="bigtitle" format="string" /> </declare-styleable> </resources>