Android 自定义View:TopBar

    zhj:topBarBackground="#00ff00">
</com.zhuanghongji.customviewzhj.view.TopBar>

TopBar.java :


import com.zhuanghongji.customviewzhj.R;

public class TopBar extends RelativeLayout {

private Button mLeftBtn;
private TextView mTitleTv;
// 左Button属性
private int leftBtnTextColor;
private Drawable leftBtnBackground;
private String leftBtnText;
// title属性
private int titleTextColor;
private String titleText;
private float titleTextSize;
// topBar属性
private Drawable topBarBackground;
// 布局属性
private LayoutParams leftBtnParams, titleParams;
// 点击事件监听器接口 -- public
public interface TopBarClickListener {
    public void leftBtnClick();
}
private TopBarClickListener listener;
// 设置监听器
public void setOnTopBarClickListener(TopBarClickListener listener) {
    this.listener = listener;
}
// 供调用的方法
public void setLeftButtonIsVisible(boolean visible) {
    if (visible) {
        mLeftBtn.setVisibility(View.VISIBLE);
    } else {
        mLeftBtn.setVisibility(View.GONE);
    }
}
// 三个构造方法,均调用均调用三个参数的那个
public TopBar(Context context) {
    this(context, null);
}
public TopBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
    // 取自定义属性 -- leftBtn
    leftBtnText = ta.getString(R.styleable.TopBar_leftBtnText);
    leftBtnTextColor = ta.getColor(R.styleable.TopBar_leftBtnTextColor, Color.WHITE);
    leftBtnBackground = ta.getDrawable(R.styleable.TopBar_leftBtnBackground);
    // 取自定义属性 -- title
    titleText = ta.getString(R.styleable.TopBar_titleText);
    titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, Color.WHITE);
    titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 12);
    // 取自定义属性 -- topBar
    topBarBackground = ta.getDrawable(R.styleable.TopBar_topBarBackground);
    //回收TypeArray(避免浪费资源,避免以为缓存导致的错误)
    ta.recycle();
    mLeftBtn = new Button(context);
    mTitleTv = new TextView(context);
    //设置自定义属性
    mLeftBtn.setText(leftBtnText);
    mLeftBtn.setTextColor(leftBtnTextColor);
    mLeftBtn.setBackground(leftBtnBackground);
    mTitleTv.setText(titleText);
    mTitleTv.setTextSize(titleTextSize);
    mTitleTv.setTextColor(titleTextColor);
    setBackground(topBarBackground);  // 可以通过此代码改变topBar背景色

// setBackgroundColor(0x00ff00); // 在这里我们直接设置成绿色

    // 设置布局 -- 左Button
    leftBtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    leftBtnParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
    leftBtnParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
    addView(mLeftBtn, leftBtnParams);
    // 设置布局 -- title
    titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
    addView(mTitleTv, titleParams);
    mLeftBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.leftBtnClick();    //左Button点击事件
        }
    });
}

}

attrs.xml :


<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="TopBar">
    <!--文字-->
    <attr name="leftBtnText" format="string"/>
    <attr name="titleText" format="string"/>
    <!--文字颜色-->
    <attr name="leftBtnTextColor" format="color"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值