android自定义view(二)创建复合控件

android创建自定义视图的第二种方式,就是创建复合控件:利用Android提供的已有控件如Button,TextView或者已经自定义好的控件 组合起来实现复杂的功能和效果,组合控件的根本就是继承ViewGroup组件
viewgroup组件是一个容器组件,里面可以容纳view或者viewgroup
1 如果你需要为你的复合控件自定义属性
需要在values目录下创建一个attrs.xml的文档

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

其中TopBar是自定义属性集合名称,里面包含了多个自定义属性,name是名称,format是属性的数据类型如:字符串型string,颜色值color,引用类型reference,表示尺寸的类型dimension 这些类型可以通过|组合使用 ,自定义属性的类型和查找这些自定义属性相对应,如下代码就有体现

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.example.com.viewmeasure.R;
public class TopBar extends RelativeLayout {
    //定义三个已有控件
    private Button mRightButton, mLeftButton;
    private TextView mTitleTextView;
    //三个控件的布局参数对象
    private LayoutParams leftParams, titleParams, rightParams;
    //第一个控件 需要设置的属性
    private String leftText;
    private int leftTextColor;
    private Drawable leftBackgroud;
    //第二个控件 需要设置的属性
    private String rightText;
    private int rightTextColor;
    private Drawable rightBackgroud;
    //第三个控件 需要设置的属性
    private String titleText;
    private int titleColor;
    private float titleTextSize;
    //定义一个内部监听器
    private TopBarListener topBarListener;


    public TopBar(Context context) {
        super(context);
    }
//实现构造方法
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(0xFFF59563);
        //TypedArray是用来存储自定义属性的数组对象
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        //从数组对象中得到Drawable类型的属性
        //注意TopBar_leftBackground和attrs文档中的命名方式
        leftBackgroud = a.getDrawable(R.styleable.TopBar_leftBackground);
        leftText = a.getString(R.styleable.TopBar_leftText);
        leftTextColor = a.getColor(R.styleable.TopBar_leftTextColor, 0);
        //第二个
        rightBackgroud = a.getDrawable(R.styleable.TopBar_rightBackground);
        rightText = a.getString(R.styleable.TopBar_rightText);
        //得到Color类型的属性
        rightTextColor = a.getColor(R.styleable.TopBar_rightTextColor, 0);
        //第三个
        titleText = a.getString(R.styleable.TopBar_title);
        titleColor = a.getColor(R.styleable.TopBar_titleTextColor, 0);
        //得到尺寸类型的属性
        titleTextSize = a.getDimension(R.styleable.TopBar_titleTextSize, 10);
        //资源回收操作很重要
        a.recycle();//资源回收
        mLeftButton = new Button(context);
        mRightButton = new Button(context);
        mTitleTextView = new TextView(context);
        //设置资源
        mLeftButton.setBackground(leftBackgroud);
        mLeftButton.setText(leftText);
        mLeftButton.setTextColor(leftTextColor);

        mRightButton.setBackground(rightBackgroud);
        mRightButton.setText(rightText);
        mRightButton.setTextColor(rightTextColor);

        mTitleTextView.setText(titleText);
        mTitleTextView.setTextColor(titleColor);
        mTitleTextView.setTextSize(titleTextSize);
        mTitleTextView.setGravity(Gravity.CENTER);
    //设置布局参数 并将控件添加到ViewGroup
        leftParams = new LayoutParams(50, LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(mLeftButton, leftParams);

        titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(mTitleTextView, titleParams);

        rightParams = new LayoutParams(50, LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(mRightButton, rightParams);

        //给内部控件设置监听器,这里处理了 我们自定义接口中的事件处理方法
        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                topBarListener.leftClick();
            }
        });
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                topBarListener.rightClick();
            }
        });

    }
    //提供给外部的设置监听器方法 ,接收一个TopBarListener
    public void setTopBarListener(TopBarListener t) {
        topBarListener = t;
    }
    //自定义TopBarListener接口,内含两个抽象方法
    //调用者需要自己实现这两个抽象方法
    public interface TopBarListener {
        void leftClick();
        void rightClick();
    }
}

布局文件

    <app.example.com.viewmeasure.view.TopBar
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/topbar"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        custom:leftBackground="@color/colorPrimaryDark"
        custom:leftText="左边"
        custom:leftTextColor="#FFFFFF"
        custom:title="微信"
        custom:titleTextColor="#FFFFFF"
        custom:rightBackground="@color/colorPrimaryDark"
        custom:rightText="右边"
        custom:rightTextColor="#FFFFFF"
        >
    </app.example.com.viewmeasure.view.TopBar>

自定义组件使用时,必须包含完整的路径
注意那个命名空间:

xmlns:custom="http://schemas.android.com/apk/res-auto"

如果没有上行的指定,那么在使用 custom:rightText=”右边”就会报错
custom 是自定义的属性的命名空间,什么是命名空间呢,我们在XML中使用一个控件的时候,

<Button
android:text=""
android:textColor=""
/>

这里的android字段 就是命名空间,它表示这个属性的来源,也就是作者

在activity中使用

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import app.example.com.viewmeasure.view.TopBar;
public class MainActivity extends AppCompatActivity {
    private TopBar topBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topBar= (TopBar) findViewById(R.id.topbar);
        topBar.setTopBarListener(new TopBar.TopBarListener() {
            @Override
            public void leftClick() {
                Toast.makeText(getApplicationContext(),"我是左边",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void rightClick() {
                Toast.makeText(getApplicationContext(),"我是右边",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值