自定义组合控件

在Android开发中经常遇到需要自定义一些组件来实现自己想要的效果,比如下面的这个效果


这个在设置中最常见,当然也可以用一个RelativeLayout来实现,不过当内容过多的时候使用自定义组合控件是最好的选择。

代码如下:

ui_setting_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="68dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_setting_view__title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置标题"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tv_setting_view__content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/tv_setting_view__title"
        android:layout_below="@id/tv_setting_view__title"
        android:text="设置内容"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:clickable="false"
        android:focusable="false"
        android:gravity="center_vertical" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/listview_devider" />

</RelativeLayout>


attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="setting_view_style">
        <attr name="title" format="string"></attr>
        <attr name="content" format="string"></attr>
        <attr name="check_text" format="string"></attr>
        <attr name="uncheck_text" format="string"></attr>
    </declare-styleable>

</resources>

SettingView.java

public class SettingView extends RelativeLayout {
	private View view;
	private TextView tvTitle, tvContent;
	private CheckBox cbStaus;
	private String check_text, uncheck_text;

	public SettingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		view = View.inflate(context, R.layout.ui_setting_view, this);
	}

	public SettingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
		// 把属性集和我们自己定义的属性集合建立映射关系
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.setting_view_style);
		String content = typedArray
				.getString(R.styleable.setting_view_style_content);
		String title = typedArray
				.getString(R.styleable.setting_view_style_title);
		check_text = typedArray
				.getString(R.styleable.setting_view_style_check_text);
		uncheck_text = typedArray
				.getString(R.styleable.setting_view_style_uncheck_text);
		tvTitle.setText(title);
		tvContent.setText(uncheck_text);
		typedArray.recycle();
	}

	private void initView(Context context) {
		view = View.inflate(context, R.layout.ui_setting_view, this);
		tvTitle = (TextView) findViewById(R.id.tv_setting_view__title);
		tvContent = (TextView) findViewById(R.id.tv_setting_view__content);
		cbStaus = (CheckBox) findViewById(R.id.cb);
	}

	public SettingView(Context context) {
		super(context);
		// 创建两个TextView 一个checkbox
		view = View.inflate(context, R.layout.ui_setting_view, this);
	}

	/**
	 * 
	 * @return 自定义控件checkBox的选中状态
	 */
	public boolean isChecked() {
		return cbStaus.isChecked();
	}

	/**
	 * 
	 * @param checked
	 *            cb状态
	 */
	public void setChecked(boolean checked) {
		cbStaus.setChecked(checked);
		if (checked) {
			setContext(check_text);
		}else {
			setContext(uncheck_text);
		}
	}

	/**
	 * 设置内容
	 * 
	 * @param text
	 */
	public void setContext(String text) {
		tvContent.setText(text);
	}

}

activity_setting.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zhong="http://schemas.android.com/apk/res/com.zhong.mobilesafe"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SettingActivity" >

    <TextView
        style="@style/text_title_style"
        android:text="设置中心" />

    <com.zhong.mobilesafe.ui.SettingView
        android:id="@+id/sv_setting_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        zhong:check_text="自动更新已开启"
        zhong:content="自动更新没有开启"
        zhong:title="设置自动更新"
        zhong:uncheck_text="自动更未已开启" >
    </com.zhong.mobilesafe.ui.SettingView>

</LinearLayout>


在这里如果要重复使用这个自定义的组件只需要复制一下代码就可以了

<com.zhong.mobilesafe.ui.SettingView
        android:id="@+id/sv_setting_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        zhong:check_text="自动更新已开启"
        zhong:content="自动更新没有开启"
        zhong:title="设置自动更新"
        zhong:uncheck_text="自动更未已开启" >
    </com.zhong.mobilesafe.ui.SettingView>



总结以上自定义组件的步骤:

1、写一个类继承ViewGroup

2、重写构造方法

3、直接在xml或者代码里面就可以使用这个自定义的view对象

4、添加自定义的属性,values目录attrs文件创建declare-styleable自定义属性

5、在R文件中自动生成我们自己定义的属性

6、申明命名空间     xmlns:<自定义tag>="http://schemas.android.com/apk/res/程序包名"

7、tag:attr=""

8、在代码的构造方法里面读取自定义的配置

//把属性集和我们自己定义的属性集合简历映射关系

9、通过代码设置读取到的信息


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章