比较简单的自定义标题栏,这里直接封装成一个类似控件的样子,先上效果图:
我们可以先写一个BaseView来,用来做标题栏的基础布局:
/**
* 自定义View的基类
* @author
*/
public class BaseView extends FrameLayout implements OnClickListener {
protected Activity mActivity;
protected Context mContext;
protected View mView;
protected LayoutInflater mInflater;
private TextView errorTV;
private boolean isErrorViewShow;
public BaseView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BaseView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BaseView(Context context) {
super(context);
}
protected void setContentView(int layoutId) {
mContext = getContext();
if (mContext instanceof Activity) {
mActivity = (Activity) mContext;
}
mInflater = LayoutInflater.from(mContext);
mView = mInflater.inflate(layoutId, null);
addView(mView);
}
protected boolean setContentView(int layoutId,int color) {
//这个方法用于后面完善操作
mContext = getContext();
if (mContext instanceof Activity) {
mActivity = (Activity) mContext;
}
mInflater = LayoutInflater.from(mContext);
mView = mInflater.inflate(layoutId, null);
mView.setBackgroundColor(color);
addView(mView); return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void addErrorView(Context context, String text){
if (null == errorTV) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
errorTV = new TextView(context);
errorTV.setLayoutParams(layoutParams);
errorTV.setVisibility(View.VISIBLE);
errorTV.setTextSize(15);
errorTV.setTextColor(Color.GRAY);
}
errorTV.setText(text);
addView(errorTV);
isErrorViewShow = true;
}
public void removeErrorView(){
if (null != errorTV) {
removeView(errorTV);
}
isErrorViewShow = false;
}
public boolean isErrorViewShow(){
return isErrorViewShow;
}
}
然后让真正的标题栏布局继承上面的类:
/**
* Created by lan.zheng on 2016/9/19.
*/
public class CommonTitleView extends BaseView{
private ImageButton backIBtn;
private TextView leftTV;
private TextView titleTV;
private ImageButton addIBtn;
private TextView rightTV;
p