1.自定义TitleBar的属性,在values中创建atts的xml文件,添加自己想要的属性。
<attr name="leftText" format="string" />
<attr name="leftTextColor" format="color" />
<attr name="leftTextSize" format="dimension" />
<attr name="leftBaclground" format="color|reference" />
<attr name="rightText" format="string" />
<attr name="rightTextColor" format="color" />
<attr name="rightTextSize" format="dimension" />
<attr name="rightBaclground" format="color|reference" />
2.新建自定义的TitleBar类继承RelativeLayout;并实现带AttributeSet参数的构造方法,通过AttributeSet可以获取在xml中设置的各个属性的值并赋给各个控件。
public class TopBar extends RelativeLayout {
private Button leftButton, rightButton;
private TextView Title;
private String leftText;
private int leftTextColor;
private float leftTextSize;
private Drawable leftBackground;
private String rightText;
private int rightTextColor;
private float rightTextSize;
private Drawable rightBackground;
private String titleText;
private int titleColor;
private float titleSize;
private Drawable titleBackground;
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.titleBar);
leftText = ta.getString(R.styleable.titleBar_leftText);
leftTextColor = ta.getColor(R.styleable.titleBar_leftTextColor, 0);
leftTextSize = ta.getDimension(R.styleable.titleBar_leftTextSize, 0);
leftBackground = ta.getDrawable(R.styleable.titleBar_leftBaclground);
rightText = ta.getString(R.styleable.titleBar_rightText);
rightTextColor = ta.getColor(R.styleable.titleBar_rightTextColor, 0);
rightTextSize = ta.getDimension(R.styleable.titleBar_rightTextSize, 0);
rightBackground = ta.getDrawable(R.styleable.titleBar_rightBaclground);
titleText = ta.getString(R.styleable.titleBar_titleBarText);
titleColor = ta.getColor(R.styleable.titleBar_titleBarTextColor, 0);
titleSize = ta.getDimension(R.styleable.titleBar_titleBarTextSize, 0);
titleBackground = ta.getDrawable(R.styleable.titleBar_titleBarBaclground);
leftButton = new Button(context);
rightButton = new Button(context);
Title = new TextView(context);
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
leftButton.setTextSize(leftTextSize);
leftButton.setBackground(leftBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setTextSize(rightTextSize);
rightButton.setBackground(rightBackground);
Title.setText(titleText);
Title.setTextColor(titleColor);
Title.setTextSize(titleSize);
Title.setBackground(titleBackground);
}
3.使用Relative的LayoutParams把各个控件放置到各自的位置,并添加到TitleBar中
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
rightParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
addView(rightButton, rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(Title, titleParams);
4.此时,你就可以在布局文件中使用过自己定义的titlebar了,不过还有个重要的功能没有实现,就是左右两个按钮的点击事件,这个我们可以通过自定义监听器来实现,在Titlebar中自定义一个TitleBarListener接口,
interface TitleBarListener {
void leftClick();
void rightClick();
}
然后通过实现这个接口控制左右连个按钮的点击事件
private TitleBarListener listener;
public void setLeftRightClickListener(TitleBarListener listener) {
this.listener = listener;
}
private void setListener() {
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.leftClick();
}
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.rightClick();
}
}
});
}
到此为止,基本的功能就可以实现了,但考虑到每个页面的titleBar可能有的有,有的米有,有的只有左边,有的只有右边….,情况比较多,所有你还可以根据自己的情况在TitleBar中增加显示的内容的方法,如:
private void isShowLeft(boolean isShowLeft) {
if (isShowLeft) {
leftButton.setVisibility(View.VISIBLE);
} else {
leftButton.setVisibility(View.GONE);
}
}