创建复合控件

来源:互联网 发布:思快奇软件下载 编辑:程序博客网 时间:2024/05/22 15:09

以创建一个TopBar为例,做一个整理

定义属性:在res资源目录的values目录下创建一个arrts.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="color|reference"/>        <attr name="leftText" format="string"/>        <attr name="rightTextColor" format="color"/>        <attr name="rightText" format="string"/>        <attr name="rightBackground" format="color|reference"/>    </declare-styleable>    </resources>

 
我们需要新建一个类继承自ViewGroup或者其子类,然后重写它的构造函数,其中一个参数的构造是在代码中使用布局时候调用的,两个参数构造是在布局文件中使用时调用的,这里重写两个参数的构造方法

public class TopBar extends RelativeLayout {    private TypedArray ta;    private String mTitle;    private float mTitleTextSize;    private int mTitleTextColor;    private int mLeftTextColor;    private Drawable mLeftBackground;    private String mLeftText;    private int mRightTextColor;    private Drawable mRightBackground;    private String mRightText;    private Button mLeftButton;    private Button mRightButton;    private TextView mTitleView;    private Context context;    private LayoutParams mLeftParams;    private LayoutParams mRightParams;    private LayoutParams mTitleParams;    private topbarClickListener mListener;    public TopBar(Context context) {        super(context);    }    public TopBar(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        //通过这个方法,将在attrs.xml中定义的declare-styleable的所有属性的值存储到TypedArray中        ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);        init();    }    private void init() {        //从TypedArray中取出对应的值来为要设置的属性赋值        mTitle = ta.getString(R.styleable.TopBar_title);        mTitleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 10);        mTitleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);        mLeftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);        mLeftText = ta.getString(R.styleable.TopBar_leftText);        mLeftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);        mRightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);        mRightText = ta.getString(R.styleable.TopBar_rightText);        mRightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);        //获取玩TypedArray的值后,一般要调用recycle方法来避免重新创建的时候的错误        ta.recycle();        mLeftButton = new Button(context);        mRightButton = new Button(context);        mTitleView = new TextView(context);        //为创建的组件元素赋值        //值就来源于我们在引用的xml文件中给对应属性的赋值        mLeftButton.setText(mLeftText);        mLeftButton.setBackground(mLeftBackground);        mLeftButton.setTextColor(mLeftTextColor);        mRightButton.setText(mRightText);        mRightButton.setTextColor(mRightTextColor);        mRightButton.setBackground(mRightBackground);        mTitleView.setText(mTitle);        mTitleView.setTextColor(mTitleTextColor);        mTitleView.setTextSize(mTitleTextSize);        mTitleView.setGravity(Gravity.CENTER);        //为组件元素设置相应的布局元素        mLeftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);        //添加到ViewGroup        addView(mLeftButton, mLeftParams);        mRightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);        addView(mRightButton, mRightParams);        mTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);        addView(mTitleView, mTitleParams);            }}

如何给这两个左右的按钮设计点击事件?

1.定义接口

    //接口对象,实现回调机制,在回调方法中,通过映射的接口对象调用接口中的方法,而不用去考虑如何实现,具体的逻辑实现由调用者创建    public interface topbarClickListener {        //左按钮点击事件        void leftClick();        //右按钮点击事件        void rightClick();    }

2.暴露方法给调用者

    //暴露一个方法给调用者来注册接口回调,通过接口来获得对叼着接口方法的实现    public void setOnTopbarClickListener(topbarClickListener mListener) {        this.mListener = mListener;    }
3.还需要实现按钮的点击事件

        //按钮的点击事件,不需要具体的实现,只需要调用接口的方法,回调的时候,会有具体的实现        mLeftButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                mListener.leftClick();            }        });


这样一个简单的复合控件就创建完成了,如何在布局文件中使用呢?

首先要定义一个命名空间,例如cunstom,然后在布局文件中加入xmlns:custom="http://schemas.android.com/apk/res-auto"即可。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:custom="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.autoviewtest.MainActivity">    <com.example.autoviewtest.TopBar        android:id="@+id/topBar"        android:layout_width="match_parent"        android:layout_height="40dp"        custom:leftBackground="@mipmap/ic_launcher"        custom:leftText="Back"        custom:leftTextColor="#FFFFFF"        custom:rightBackground="@mipmap/ic_launcher"        custom:rightText="More"        custom:rightTextColor="#FFFFFF"        custom:title="自定义标题"        custom:titleTextColor="#123412"        custom:titleTextSize="10sp"/></LinearLayout>




原创粉丝点击