自定义控件:自定义组合控件+自定义属性

自定义组合控件:

就是将几个基本控件放到一个控件view中,该view需要extends 五大布局中的任何一种,自定义组合控件一般和自定义属性共同出现。

Demo

自定义组合控件包含2个TextView,1个CheckBox,把这3个view用1个view来显示并控制。

效果图

这里写图片描述

步骤

  • 1 创建自定义组合控件的布局和类,并在layout节点下使用,
  • 2 创建自定义属性并在layout中使用
  • 3 在自定义的类中找到子view,拿到属性值,并设置值
  • 4 给组合控件的父控件 提供set和get方法来管理组合控件
  • 5 父控件设置自定义组合控件

效果图
这里写图片描述

项目结构图:

这里写图片描述
attr.xml:

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

    <declare-styleable name="CustomView">
        <attr name="title" format="reference|string" />
        <attr name="desc" format="reference|string" />
        <attr name="checked" format="boolean" />
    </declare-styleable>

</resources>
<!--  参照sdk\platforms\android-18\data\res\values\attrs.xml-->
<!--   <attr name="backgroundDimEnabled" format="boolean" /> -->
<!--   <attr name="candidatesTextStyleSpans" format="reference|string" /> -->

在activity_main.xml中使用自定义属性

这里写图片描述

CustomView的布局:item_main.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="70dp"
    android:background="#85b14e"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="title"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:gravity="center_vertical"
        android:text="desc"
        android:textSize="20sp" />

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

</RelativeLayout>

自定义的view:

package com.android.customsgroupview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomView extends LinearLayout { // extends任一五大布局
    private View customView;
    private TextView tv_title;
    private TextView tv_desc;
    private CheckBox checkbox;
    private String title;
    private Boolean checked;
    private String desc;

    // 有自定义属性,构造方法要用2个参数的。
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        customView = View.inflate(context, R.layout.item_main, this);

        /**
         * 到这里我们在activity_main.xml设置自定义的属性并生效,因为CustomView不知道应该把值给哪一个子view
         * 要想让自定义的属性生效,需到设置,就像:textView.setText("hello")
         */
        initAttrs(attrs);
        initView(context);
    }

    /**
     * 设置自定义属性
     * @param attrs
     *            1 声明属性attrs.xml
     *            2 在父布局activity_main.xml中使用 
     *            3 取值
     */
    private void initAttrs(AttributeSet attrs) {
        // xmlns:customview="http://schemas.android.com/apk/res/com.android.customsgroupview"
        String namespace = "http://schemas.android.com/apk/res/com.android.customsgroupview";
        checked = attrs.getAttributeBooleanValue(namespace, "checked", false);
        title = attrs.getAttributeValue(namespace, "title");
        desc = attrs.getAttributeValue(namespace, "desc");
    }

    /**
     * 默认设置
     * @param context
     */
    private void initView(Context context) {
        tv_title = (TextView) customView.findViewById(R.id.tv_title);
        tv_desc = (TextView) customView.findViewById(R.id.tv_desc);
        checkbox = (CheckBox) customView.findViewById(R.id.checkbox);

        //默认设置的值
        tv_title.setText(title);
        boolean isUpdate = context.getSharedPreferences("config",context.MODE_PRIVATE).getBoolean("isUpdate", false);
        checkbox.setChecked(isUpdate);
        if (isUpdate) {
            tv_desc.setText("自动更新设置已开启");
            tv_desc.setTextColor(Color.parseColor("#000000"));
        } else {
            tv_desc.setText("自动更新设置已关闭");
            tv_desc.setTextColor(Color.parseColor("#e60200"));
        }
    }

    /**
     * 对外提供设置自定义控件的方法
     * @param isUpdate
     */
    public void setCustomView(Boolean isUpdate) {
        checkbox.setChecked(isUpdate);
        if (isUpdate) {
            tv_desc.setText("自动更新设置已开启");
            tv_desc.setTextColor(Color.parseColor("#000000"));
        } else {
            tv_desc.setText("自动更新设置已关闭");
            tv_desc.setTextColor(Color.parseColor("#e60200"));
        }
    }

    /**
     *  对外提供获取是否自动更新的方法
     * @return
     */
    public Boolean getIsUpdate(){
        return checkbox.isChecked();
    }
}

MainActivity

package com.android.customsgroupview;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
/**
 * 创建自定义组合控件 自定义属性的使用
 * @author cui
 * 1 创建自定义组合控件的布局和类,并在layout节点下使用,
 * 2 创建自定义属性并在layout中使用
 * 3 在自定义的类中找到子view,拿到属性值,并设置值
 * 4 给组合控件的父控件 提供set和get方法来管理组合控件
 * 5 父控件设置
 */
public class MainActivity extends Activity {

    private CustomView customView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        customView = (CustomView) findViewById(R.id.customview);
        customView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                boolean isUpdate = getSharedPreferences("config",MODE_PRIVATE).getBoolean("isUpdate", false);

                customView.setCustomView(!isUpdate);

                Editor edit = getSharedPreferences("config",MODE_PRIVATE).edit();
                edit.putBoolean("isUpdate", !isUpdate);
                edit.commit();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Demo:http://download.csdn.net/detail/ss1168805219/9500125

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值