创建组合控件通常要继承ViewGroup,我们一般会给它指定一些可配置的属性,让它具有更强的扩展性。
自定义一个如下图所示的组合控件:
该控件有三个部分组成,左边和右边是一个Button,中间是一个TextView,并为该控件添加监听事件。
1.定义属性,在res的Values目录下创建一个attrs.xml的属性定义文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar"><!--自定义组合控件的名字-->
<!--定义各种属性-->
<!--中间的部分-->
<attr name="mtitle" format="string"></attr>
<attr name="titleTextSize" format="dimension"></attr>
<attr name="mtitleTextColor" format="color"></attr>
<!--左边的部分-->
<attr name="leftText" format="string"></attr>
<attr name="leftTextColor" format="color"></attr>
<attr name="leftBackground" format="reference|color"></attr>
<!--右边的控件-->
<attr name="rightText" format="string"></attr>
<attr name="rightTextColor" format="color"></attr>
<attr name="rightBackGround" format="reference|color"></attr>
</declare-styleable>
</resources>
我们定义了控件名字,字体颜色,背景3个属性,format是值该属性的取值类型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
reference:参考某一资源ID,color:颜色值,boolean:布尔值,dimension:尺寸值,float:浮点值
integer:整型值,string:字符串,fraction:百分数, enum:枚举值, flag:位或运算
package com.hybunion.customview.view; 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 com.hybunion.customview.R; /** * Created by Hbj on 2016/8/19. */ public class TopBar extends RelativeLayout { // 包含topbar上的元素:左按钮、右按钮、标题 private Button mLeftButton, mRightButton; private TextView mTitleView; // 布局属性,用来控制组件元素在ViewGroup中的位置 private LayoutParams mLeftParams, mTitlepParams, mRightParams; // 左按钮的属性值,即我们在atts.xml文件中定义的属性 private int mLeftTextColor; private Drawable mLeftBackground; private String mLeftText; // 右按钮的属性值,即我们在atts.xml文件中定义的属性 private int mRightTextColor; private Drawable mRightBackground; private String mRightText; // 标题的属性值,即我们在atts.xml文件中定义的属性 private float mTitleTextSize; private int mTitleTextColor; private String mTitle; // 映射传入的接口对象 private topbarClickListener mListener; public TopBar(Context context) { super(context); } public TopBar(Context context, AttributeSet attrs) { super(context, attrs); // 设置topbar的背景 setBackgroundColor(0xFFF59563); /** * 通过这个方法,将你在atts.xml中定义的declare-styleable的所有属性的值存储到TypedArray中 */ TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar); //从TypedArray中取出对应的值来为要设置的属性赋值 mLeftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0); mLeftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground); mLeftText = ta.getString(R.styleable.TopBar_leftText); mRightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0); mRightBackground = ta.getDrawable(R.styleable.TopBar_rightBackGround); mRightText = ta.getString(R.styleable.TopBar_rightText); mTitleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 10); mTitleTextColor = ta.getColor(R.styleable.TopBar_mtitleTextColor, 0); mTitle = ta.getString(R.styleable.TopBar_mtitle); // 获取完TypedArray的值后,一般要调用 // recyle方法来避免重新创建的时候的错误 ta.recycle(); mLeftButton = new Button(context); mRightButton = new Button(context); mTitleView = new TextView(context); // 为创建的组件元素赋值 // 值就来源于我们在引用的xml文件中给对应属性的赋值 mLeftButton.setTextColor(mLeftTextColor); mLeftButton.setBackground(mLeftBackground); mLeftButton.setText(mLeftText); mRightButton.setTextColor(mRightTextColor); mRightButton.setBackground(mRightBackground); mRightButton.setText(mRightText); mTitleView.setText(mTitle); mTitleView.setTextSize(mTitleTextSize); mTitleView.setTextColor(mTitleTextColor); 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); //中间的控件 mTitlepParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); mTitlepParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(mTitleView, mTitlepParams); // 按钮的点击事件,不需要具体的实现, // 只需调用接口的方法,回调的时候,会有具体的实现 mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListener.leftClick(); } }); mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListener.rightClick(); } }); } public TopBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // 暴露一个方法给调用者来注册接口回调 // 通过接口来获得回调者对接口方法的实现 public void setOnTopbarClickListener(topbarClickListener mListener) { this.mListener = mListener; } // 接口对象,实现回调机制,在回调方法中 // 通过映射的接口对象调用接口中的方法 // 而不用去考虑如何实现,具体的实现由调用者去创建 public interface topbarClickListener { //左按钮点击事件 void leftClick(); //右按钮点击事件 void rightClick(); } /** * 设置按钮的显示与否 通过id区分按钮,flag区分是否显示 * * @param id id * @param flag 是否显示 */ public void setButtonVisable(int id, boolean flag) { if (flag) { if (id == 0) { mLeftButton.setVisibility(View.VISIBLE); } else { mRightButton.setVisibility(VISIBLE); } } else { if (id == 0) { mLeftButton.setVisibility(GONE); } else { mRightButton.setVisibility(GONE); } } } }3
注意
xmlns:
custom
=
"http://schemas.android.com/apk/res-auto"
在需要的地方引用第三方控件的名字空间。
在Android Studio中,第三方控件都使用如下代码来引入名字空间
xmlns:custom="http://schemas.android.com/apk/res-auto"将引入的第三方控件的名字空间取为custom,之后在xml文件中使用自定义的属性时,就可以通过这个名字空间来引用。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="5dp" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hybunion.customview.activity.AssembleActivity"> <com.hybunion.customview.view.TopBar android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="40dp" custom:leftBackground="@drawable/blue_button" custom:leftText="Back" custom:leftTextColor="#FFFFFF" custom:rightBackGround="@drawable/blue_button" custom:rightText="More" custom:rightTextColor="#FFFFFF" custom:title="自定义标题" custom:titleTextColor="#123412" custom:titleTextSize="10sp"/> </RelativeLayout>
package com.hybunion.customview.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.hybunion.customview.R; import com.hybunion.customview.view.TopBar; public class AssembleActivity extends AppCompatActivity { private TopBar mTopbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_assemble); // 获得我们创建的topbar mTopbar = (TopBar) findViewById(R.id.topBar); // 为topbar注册监听事件,传入定义的接口 // 并以匿名类的方式实现接口内的方法 mTopbar.setOnTopbarClickListener(new TopBar.topbarClickListener() { @Override public void leftClick() { Toast.makeText(AssembleActivity.this, "left", Toast.LENGTH_SHORT) .show(); } @Override public void rightClick() { Toast.makeText(AssembleActivity.this, "right", Toast.LENGTH_SHORT) .show(); } }); } }