自定义View之组合控件

第一步:定义属性—-在res下的values目录下创建一个attrs.xml的文件,定义一些自己用到的属性

有关属性的详解

<resources>
    <declare-styleable name="TopBar">
        <attr name="title" format="string"></attr>
        <attr name="titleTextSize" format="dimension"></attr>
        <attr name="titleTextColor" format="color"></attr>
        <attr name="leftTextColor" format="color"></attr>
        <attr name="leftBackground" format="reference|color"></attr>
        <attr name="leftText" format="string"></attr>
        <attr name="rightTextColor" format="color"></attr>
        <attr name="rightBackground" format="reference|color"></attr>
        <attr name="rightText" format="string"></attr>
    </declare-styleable>
</resources>

定义完属性后,新建一个自定义的类继承ViewGroup.这里继承至RelativeLayout

/**
 * 组合控件
 */
public class CompoundControl extends RelativeLayout{
    private topbarClickListener topbarClickListener;
    private Button leftButton,rightButton;
    private TextView mTextTitle;
    private String leftText,mRightText,mTitleText;
    private int leftTextColor,mRightTextColor,mTitleTextColor;
    private float mTitleTextSize;
    private Drawable mRightBackground,mLeftBackground;
    private LayoutParams LeftlayoutParams,rightLayoutParams,TitleLayoutParams;
    public CompoundControl(Context context) {
        super(context);

    }

    public CompoundControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);//记得调用初始化
    }

    public CompoundControl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);//记得调用初始化
    }

    /**
     * 获取自定义的属性 设置属性参数
     * @param context
     * @param attrs
     */
    private void init(Context context,AttributeSet attrs){
        /**
         * 第一步引用自定义属性
         */
        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
        /**
         * 赋值  
         */

        //左边的按钮
        leftTextColor = array.getColor(R.styleable.TopBar_leftTextColor,0);
        leftText=array.getString(R.styleable.TopBar_leftText);
        mLeftBackground=array.getDrawable(R.styleable.TopBar_leftBackground);

        //右边的
        mRightBackground=array.getDrawable(R.styleable.TopBar_rightBackground);
        mRightText=array.getString(R.styleable.TopBar_rightText);
        mRightTextColor=array.getColor(R.styleable.TopBar_rightTextColor,0);

        //标题
        mTitleText=array.getString(R.styleable.TopBar_title);
        mTitleTextColor=array.getColor(R.styleable.TopBar_titleTextColor,0);
        mTitleTextSize=array.getDimension(R.styleable.TopBar_titleTextSize,10);
        /**
         * 记得调用recycler避免重新创建的时候错误
         */
        array.recycle();//回收

        /**
         * 自定义三个控件
         */
        leftButton=new Button(context);
        rightButton=new Button(context);
        mTextTitle=new TextView(context);

        leftButton.setText(leftText);
        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(mLeftBackground);

        rightButton.setText(mRightText);

        mTextTitle.setText(mTitleText);
        mTextTitle.setTextColor(mTitleTextColor);
        mTextTitle.setTextSize(mTitleTextSize);
        mTextTitle.setGravity(Gravity.CENTER);

        /**
         * 设置控件的大小
         */
        LeftlayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LeftlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        /**
         * 添加到ViewGroup
         */
        addView(leftButton,LeftlayoutParams);

        rightLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightLayoutParams);

        TitleLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(mTextTitle,TitleLayoutParams);
        setOnClickListener();


    }

    /**
     * 设置点击事件  我在这里只写了左边的点击事件
     * 自定义一个接口  通过映射接口去调用实现方法
     */
    private void setOnClickListener(){
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                topbarClickListener.leftClick();
            }
        });
    }


    /**
     * 给外部提供一个调用的方法
     * @param on
     */
    public void setTopbarClickListener(topbarClickListener on
    ){
        this.topbarClickListener=on;
    }

点击事件接口

public interface topbarClickListener {

    void leftClick();
    void rightClick();
}

引用自定义的类—注意名字空间 xmlns:cumtor可以随便起名

<wifi.yeeloor.com.scoller.CompoundControl
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cumtor="http://schemas.android.com/apk/res-auto"
    android:id="@+id/topBar"
    cumtor:leftText="Back"
    cumtor:leftTextColor="#000000"
    cumtor:title="自定义标题"
    cumtor:titleTextSize="15sp"
    cumtor:titleTextColor="#123412"
    cumtor:rightText="More"
    android:layout_width="match_parent"
    android:layout_height="40dp"></wifi.yeeloor.com.scoller.CompoundControl>
<!--记得属性必须全写-->

然后通过引用<include>调用到相应的位置

    <include layout="@layout/topbar" android:id="@+id/mainTopbar"></include>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值