创建复合控件可以良好的创建出具有重用功能的控件集合.我们一般会给他制定一些可配置的属性,让他更具有强的扩展性.
定义属性
<?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>
接下来在ViewGroup中获取attrs中的值 此时的ViewGroup为RelativeLayout
public class TopBarView extends RelativeLayout {
private int mLeftTextColor;
private Drawable mLeftBackground;
private String mLeftText;
private int mRightTextColor;
private Drawable mRightBackground;
private String mRightText;
private float mTitleTextSize;
private int mTitleColor;
private String mTitle;
public TopBarView(Context context) {
super(context);
}
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取XML布局文件中的自定义属性
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
//通过getString getColor等方法获取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
);
mTitleColor=ta.getColor(
R.styleable.TopBar_titleTextColor,0
);
mTitle=ta.getString(
R.styleable.TopBar_title
);
//获取完TypedArray的值之后,一般要调用recyle方法来避免重复创建时候的错误
ta.recycle();
}
}
组合控件
mLeftButton.setText(mLeftText);
mLeftButton.setTextColor(mLeftTextColor);
mLeftButton.setBackground(mLeftBackground);
mRightButton.setText(mRightText);
mRightButton.setTextColor(mRightTextColor);
mRightButton.setBackground(mRightBackground);
mTitleTextView.setText(mTitle);
mTitleTextView.setTextColor(mTitleColor);
mTitleTextView.setTextSize(mTitleTextSize);
mTitleTextView.setGravity(Gravity.CENTER);
//为组件添加相应的布局元素
//左侧的参数
mLeftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
//添加到ViewGroup中
addView(mLeftButton,mLeftParams);
mRightParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(mRightButton,mRightParams);
mTitleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(mTitleTextView,mTitleParams);
设置点击事件和回调
//按钮的点击事件
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 void setOnTopBarClickListener(topbarClickListener mListener){
this.mListener=mListener;
}
public void setButtonVisable(int id,boolean flag){
if (flag){
if (id==0){
mLeftButton.setVisibility(View.VISIBLE);
}else {
mRightButton.setVisibility(View.VISIBLE);
}
}else {
if (id==0){
mLeftButton.setVisibility(View.GONE);
}else {
mRightButton.setVisibility(View.GONE);
}
}
}