自定义TitleBar的母版

本文介绍如何在Android应用中自定义TitleBar组件,包括定义属性、实现构造方法、设置布局及添加点击事件等步骤。

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);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值