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"/>
|