自定义组合控件 定义类继承RelativeLayout
public class TopBar extends RelativeLayout {
//topBar上的元素
private Button leftButton, rightButton;
private TextView mTitle;
//布局属性,控制元素在viewgroup的位置
private LayoutParams lParams, rParams, mParams;
//中间文字的属性值
private String title;
private float titleSize;
private int titleColor;
private Drawable titleBackgroundColor;
//左按钮的属性值
private String leftButtonText;
private float leftButtonSize;
private int leftButtonColor;
private Drawable leftButtonBackgroundColor;
//右按钮的属性值
private String rightButtonText;
private float rightButtonSize;
private int rightButtonColor;
private Drawable rightButtonBackgroundColor;
//注册接口成员属性
private TopBarClickListener topBarClickListener;
public TopBar(Context context) {
super(context);
}
public TopBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public TopBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
}
//定义控件属性的方法
private void initAttrs(Context context, AttributeSet attrs) {
setBackgroundColor(0xFFF59563);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
// 从TypedArray中取出对应的值来为要设置的属性赋值
title = typedArray.getString(R.styleable.TopBar_title);
titleSize = typedArray.getDimension(R.styleable.TopBar_titleSize,10);
titleColor = typedArray.getColor(R.styleable.TopBar_titleColor ,0);
titleBackgroundColor = typedArray.getDrawable(R.styleable.TopBar_titleBackGroundColor);
leftButtonText = typedArray.getString(R.styleable.TopBar_leftText);
leftButtonSize = typedArray.getDimension(R.styleable.TopBar_leftTextSize,10);
leftButtonColor = typedArray.getColor(R.styleable.TopBar_leftTextColor,0);
leftButtonBackgroundColor = typedArray.getDrawable(R.styleable.TopBar_leftTextBackGroundColor);
rightButtonText = typedArray.getString(R.styleable.TopBar_rightText);
rightButtonSize = typedArray.getDimension(R.styleable.TopBar_rightTextSize,10);
rightButtonColor = typedArray.getColor(R.styleable.TopBar_rightTextColor,0);
rightButtonBackgroundColor = typedArray.getDrawable(R.styleable.TopBar_rightTextBackGroundColor);
// recyle方法来避免重新创建的时候的错误
typedArray.recycle();
//实例元素组件
leftButton = new Button(context);
rightButton = new Button(context);
mTitle = new TextView(context);
//给元素组件赋值
mTitle.setText(title);
mTitle.setTextSize(titleSize);
mTitle.setTextColor(titleColor);
mTitle.setBackground(titleBackgroundColor);
leftButton.setText(leftButtonText);
leftButton.setTextSize(leftButtonSize);
leftButton.setTextColor(leftButtonColor);
leftButton.setBackground(leftButtonBackgroundColor);
rightButton.setText(rightButtonText);
rightButton.setTextSize(rightButtonSize);
rightButton.setTextColor(rightButtonColor);
rightButton.setBackground(rightButtonBackgroundColor);
//为元素组件设置布局
lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
lParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton,lParams);
mParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(mTitle,mParams);
rParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rParams);
//按钮点击监听,调用接口
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
topBarClickListener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
topBarClickListener.rightClick();
}
});
}
public interface TopBarClickListener {
//左按钮点击
void leftClick();
//右按钮点击
void rightClick();
}
//给接口定义set方法 可定义监听事件
public void setTopBarClickListener(TopBarClickListener topBarClickListener) {
this.topBarClickListener = topBarClickListener;
}
}
values文件夹下新建arrts
<declare-styleable name="TopBar">
<attr name="title" format="string">
<attr name="titleSize" format="dimension">
<attr name="titleColor" format="color">
<attr name="titleBackGroundColor" format="reference|color">
<attr name="leftText" format="string">
<attr name="leftTextSize" format="dimension">
<attr name="leftTextColor" format="color">
<attr name="leftTextBackGroundColor" format="reference|color">
<attr name="rightText" format="string">
<attr name="rightTextSize" format="dimension">
<attr name="rightTextColor" format="color">
<attr name="rightTextBackGroundColor" format="reference|color">
在布局中可直接使用定义好的控件