简单的自定义标题栏(不使用Toolbar)

这篇博客介绍了如何不使用Toolbar,通过创建一个BaseView并封装成标题栏组件,详细讲解了布局设计和在Activity中的应用,支持图标更换和背景色自定义,提供了可扩展的预留控件以适应更多需求。
摘要由CSDN通过智能技术生成

比较简单的自定义标题栏,这里直接封装成一个类似控件的样子,先上效果图:


我们可以先写一个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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值