public class CustomBar extends RelativeLayout {
String title,leftText,rightText;
int leftTextColor,rightTextColor,titleTextColor;
float titleTextSize;
Drawable leftBackground,rightBackground;
TextView titleView;
Button leftButton,rightButton;
LayoutParams leftLayout,rightLayout,titleLayout;
public CustomBar(Context context, AttributeSet attrs) {
super(context, attrs);
//获取在XML布局文件中自定义的呢些属性
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomTopBar);
//系统提供了TypedArray这样的数据结构来获取自定义属性集,接下来通过TypedArray对象的
//getString()方法等就可以获取这些自定义的属性值
title = ta.getString(R.styleable.CustomTopBar_titleBar);
leftText = ta.getString(R.styleable.CustomTopBar_leftText);
rightText = ta.getString(R.styleable.CustomTopBar_rightText);
titleTextColor = ta.getColor(R.styleable.CustomTopBar_titleBarTextColor,0);
leftTextColor = ta.getColor(R.styleable.CustomTopBar_leftTextColor,0);
rightTextColor = ta.getColor(R.styleable.CustomTopBar_rightTextColor,0);
titleTextSize = ta.getDimension(R.styleable.CustomTopBar_titleBarTextSize,10);
leftBackground = ta.getDrawable(R.styleable.CustomTopBar_leftBackground);
rightBackground = ta.getDrawable(R.styleable.CustomTopBar_rightBackground);
ta.recycle();//获取完属性值后,调用recycle()方法回收资源,来避免重新创建时的错误
leftButton = new Button(context);
rightButton = new Button(context);
titleView = new TextView(context);
titleView.setText(title);
titleView.setTextSize(titleTextSize);
titleView.setTextColor(titleTextColor);
titleView.setGravity(Gravity.CENTER);
leftButton.setText(leftText);
leftButton.setBackground(leftBackground);
leftButton.setTextColor(leftTextColor);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
titleLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
titleLayout.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(titleView,titleLayout);
leftLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
leftLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftLayout);
rightLayout = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
rightLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rightLayout);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
public CustomBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 通过id 和flag 来设置左右按钮的可见状态
* @param id
* @param flag
*/
public void setButtonVisible(int id,boolean flag){
if (flag){
if (id==0){
leftButton.setVisibility(View.VISIBLE);
}else {
rightButton.setVisibility(View.VISIBLE);
}
}else {
if (id==0){
leftButton.setVisibility(View.GONE);
}else {
rightButton.setVisibility(View.GONE);
}
}
}
private topBarClickListener listener;
//暴露一个方法给调用者来注册接口回调
//通过接口来获得回调者对接口方法的实现
public void setListener(topBarClickListener listener) {
this.listener = listener;
}
//定义接口,
//接口对象,实现回调机制,在回调方法中通过映射的接口对象
//调用接口中的方法,而不去思考如何实现,具体的实现由调用者去创建
public interface topBarClickListener{
void leftClick();
void rightClick();
}
}
在MainACtivity中调用如下
bar = (CustomBar) findViewById(R.id.topBar);
bar.setButtonVisible(0,true);
bar.setButtonVisible(1,true);
bar.setListener(new CustomBar.topBarClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "返回", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "更多", Toast.LENGTH_SHORT).show();
}
});
配置文件,values目录下新建attrs.XML文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTopBar">
<attr name="titleBar" format="string"/>
<attr name="titleBarTextSize" format="dimension"/>
<attr name="titleBarTextColor" format="color"/>
<attr name="leftText" format="string"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
自定义布局模板
<com.learn.customtopbar.CustomBar
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
custom:leftText="返回"
custom:leftTextColor="@android:color/white"
custom:rightText="更多"
custom:rightTextColor="@android:color/white"
custom:titleBarTextSize="14sp"
custom:titleBar="信息"
custom:titleBarTextColor="@android:color/white"
></com.learn.customtopbar.CustomBar>