自定义组合控件

来源:互联网 发布:建筑四棱台体积算法 编辑:程序博客网 时间:2024/06/13 10:01

1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候,直接使用组合控件对应的对

象.

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="com.example.phone.view.SettingItemView">        <attr name = "destitle" format = "string"/>        <attr name = "desoff" format = "string"/>        <attr name = "deson" format = "string"/>    </declare-styleable></resources>
2.将组合控件的布局,抽取到单独的一个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="wrap_content"         android:padding="5dp"        >        <TextView             android:id="@+id/tv_title"            android:textColor="#000"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="自动更新设置"            android:textSize="18sp"            />                <TextView             android:id="@+id/tv_des"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/tv_title"                        android:textColor="#000"            />                <CheckBox             android:id="@+id/cb_box"            android:focusable="false"            android:clickable="false"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            />                <View             android:layout_below="@id/tv_des"            android:background="#000"            android:layout_marginTop="3dp"            android:layout_width="match_parent"            android:layout_height="1dp"            />    </RelativeLayout>
3.通过一个单独的类,去加载此段布局文件.

package com.example.phone.view;import com.example.phone.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;public class SettingItemView extends RelativeLayout{private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.example.phone";private TextView tv_des;private CheckBox cb_box;private String mDestitle;private String mDeson;private String mDesoff;public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);View.inflate(context, R.layout.setting_item_view, this);TextView tv_title = (TextView) findViewById(R.id.tv_title);tv_des = (TextView) findViewById(R.id.tv_des);cb_box = (CheckBox)findViewById(R.id.cb_box);initAttrs(attrs);tv_title.setText(mDestitle);}private void initAttrs(AttributeSet attrs) {mDestitle = attrs.getAttributeValue(NAMESPACE, "destitle");mDeson = attrs.getAttributeValue(NAMESPACE, "deson");mDesoff = attrs.getAttributeValue(NAMESPACE, "desoff");}public boolean isCheck() {// TODO Auto-generated method stubreturn cb_box.isChecked();}public void setChecked(boolean checked) {// TODO Auto-generated method stubcb_box.setChecked(checked);if(checked) {tv_des.setText(mDeson);} else {tv_des.setText(mDesoff);}}public SettingItemView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public SettingItemView(Context context) {this(context,null);}}
View.inflate(context, R.layout.setting_item_view, this);
使用这行代码往该布局添加view,同listview
4.checkBox是否选中,决定SettingItemView是否开启,isCheck(){return checkbox.isCheck()}方法

点击在SettingItemView中CheckBox区域,事件就由SettingItemView传递给CheckBox,由CheckBox去做响应
CheckBox响应当前的点击事件,则SettingItemView就不能再去响应此事件,不能调用onClick方法,去改变状态

checkbox的xml文件

<CheckBox             android:id="@+id/cb_box"            android:focusable="false"            android:clickable="false"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            />

    android:focusable="false"            android:clickable="false"
这两行代码实现 
在homexml中使用控件

<com.example.phone.view.SettingItemView        xmlns:phonesafe="http://schemas.android.com/apk/res/com.example.phone"        phonesafe:destitle="自动更新设置"        phonesafe:desoff="自动更新已开启"        phonesafe:deson="自动更新已关闭"        android:id="@+id/siv_update"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </com.example.phone.view.SettingItemView>
在activity中初始化控件

final SettingItemView siv_update = (SettingItemView) findViewById(R.id.siv_update);boolean open_update = SpUtil.getBoolean(getApplicationContext(),ContantValue.OPEN_UPDATE, false);siv_update.setChecked(open_update);siv_update.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {siv_update.setChecked(!siv_update.isCheck());SpUtil.putBoolean(getApplicationContext(),ContantValue.OPEN_UPDATE, siv_update.isCheck());}});







原创粉丝点击