美文网首页
一个简单的自定义组合控件SettingItemView

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

作者: majorty | 来源:发表于2018-03-27 23:19 被阅读0次

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

 <RelativeLayout 
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/tv_title"
            android:text="自动更新设置"
            android:textColor="#000"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView 
            android:id="@+id/tv_des"
            android:layout_below="@id/tv_title"
            android:text="自动更新已关闭"
            android:textColor="#000"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <CheckBox 
            android:id="@+id/cb_box"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <View
            android:background="#000"
            android:layout_below="@id/tv_des"
            android:layout_width="match_parent"
            android:layout_height="1dp"/>
    </RelativeLayout>

实现方法:
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.getAttributeCount();i++){
            Log.i(tag, "name = "+attrs.getAttributeName(i));
            Log.i(tag, "value = "+attrs.getAttributeValue(i));
            Log.i(tag, "分割线 ================================= ");
        }*/
        
        //通过名空间+属性名称获取属性值
        
        mDestitle = attrs.getAttributeValue(NAMESPACE, "destitle");
        mDesoff = attrs.getAttributeValue(NAMESPACE, "desoff");
        mDeson = attrs.getAttributeValue(NAMESPACE, "deson");
        
        Log.i(tag, mDestitle);
        Log.i(tag, mDesoff);
        Log.i(tag, mDeson);
    }

    /**
     * 判断是否开启的方法
     * @return  返回当前SettingItemView是否选中状态   true开启(checkBox返回true)  false关闭(checkBox返回true)
     */
    public boolean isCheck(){
        //由checkBox的选中结果,决定当前条目是否开启
        return cb_box.isChecked();
    }

    /**
     * @param isCheck   是否作为开启的变量,由点击过程中去做传递
     */
    public void setCheck(boolean isCheck){
        //当前条目在选择的过程中,cb_box选中状态也在跟随(isCheck)变化
        cb_box.setChecked(isCheck);
        if(isCheck){
            //开启
            tv_des.setText(mDeson);
        }else{
            //关闭
            tv_des.setText(mDesoff);
        }
    }
    
}

attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.simon.safe.view.SettingItemView">
        <attr name="destitle" format="string"/>
        <attr name="desoff" format="string"/>
        <attr name="deson" format="string"/>
    </declare-styleable>
</resources>

使用:

 <com.simon.safe.SettingItemView
        android:id="@+id/siv_update"
        xmlns:mobilesafe="http://schemas.android.com/apk/res/com.simon.safe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        mobilesafe:desoff="自动更新已关闭"
        mobilesafe:deson="自动更新已开启"
        mobilesafe:destitle="自动更新设置">
    </com.simon.safe.SettingItemView>

相关文章

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

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

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • 11-自定义组合控件以及使用

    一、自定义组合控件介绍 开发中,为了使用的方便,经常把一些控件组合成一个控件,那样就成为了我们的自定义组合控件,严...

  • android自定义View基础

    自定义View基础1.1 分类自定义View的实现方式有以下几种 类型 定义自定义组合控件 多个控件组合成为一个...

  • Android自定义控件:带动画效果的手机号输入框 (3-4-4

    项目中很多地方,使用到了自定义控件。 简单点的,如个性控件的定制,多个组件的组合封装等。我们需要了解自定义控件的基...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • Android组合控件详解 & 自定义属性

    组合控件详解 & 自定义属性 摘录来源:极客学院WiKi 组合控件是自定义控件的一种,只不过它是由其他几个原生控件...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • 组合控件2——海贼王选项菜单

    之前的自定义控件——初识自定义控件,我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。而我们在自制控件...

  • FlutterUI(二)Canvas 与 Paint

    Flutter自定义控件分为三大类: 组合控件,通过组合其他widget成为一个新的widget。 自绘控件,通过...

网友评论

      本文标题:一个简单的自定义组合控件SettingItemView

      本文链接:https://www.haomeiwen.com/subject/wrsecftx.html