一个简单的自定义组合控件SettingItemView

将以下的相对布局,抽取到单独的一个类中去做管理,以后只需要在布局文件中添加此类,即可达到以下效果

 
        
        
        
        
    

实现方法:
1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候, 直接使用组合控件对应的对象.
2.将组合控件的布局,抽取到单独的一个xml中
3.通过一个单独的类,去加载此段布局文件.
4.checkBox是否选中,决定SettingItemView是否开启,isCheck(){return checkbox.isCheck()}方法
5.提供一个SettingItemView,切换选中状态的方法setCheck(boolean isCheck)

package com.itheima.mobilesafe74.view;

import com.itheima.mobilesafe74.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
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.itheima.mobilesafe74";
    private static final String tag = "SettingItemView";
    private CheckBox cb_box;
    private TextView tv_des;
    private String mDestitle;
    private String mDesoff;
    private String mDeson;

    public SettingItemView(Context context) {
        this(context,null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //xml--->view   将设置界面的一个条目转换成view对象,直接添加到了当前SettingItemView对应的view中
        View.inflate(context, R.layout.setting_item_view, this);

        //等同于以下两行代码
        /*View view = View.inflate(context, R.layout.setting_item_view, null);
        this.addView(view);*/
        
        //自定义组合控件中的标题描述
        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);
        
        //获取自定义以及原生属性的操作,写在此处,AttributeSet attrs对象中获取
        initAttrs(attrs);
        //获取布局文件中定义的字符串,赋值给自定义组合控件的标题
        tv_title.setText(mDestitle);
    }
    
    /**
     * 返回属性集合中自定义属性属性值
     * @param attrs 构造方法中维护好的属性集合
     */
    private void initAttrs(AttributeSet attrs) {
        /*//获取属性的总个数
        Log.i(tag, "attrs.getAttributeCount() = "+attrs.getAttributeCount());
        //获取属性名称以及属性值
        for(int i=0;i

attrs.xml文件:



    
        
        
        
    

使用:

 
    

你可能感兴趣的:(一个简单的自定义组合控件SettingItemView)