Android titleBar可以直接集成到每一个页面不需include布局文件,在BaseActivity集成。 不需要重新设置title,也不需要include。 加载页面的速度也提高很多;
一,自定义导航栏
1,通过TitleBuilder类构建自定义的的导航栏,左边显示返回键,中间的标题栏及副标题,右边显示菜单栏;
* title栏根布局 */ protected View mTitleView;// titleView protected RelativeLayout mTitleBgColor; protected RelativeLayout mTitleLeftRelativeLayout;//title左边布局 protected RelativeLayout mTitleMiddleRelativeLayout;//title中间布局 protected RelativeLayout mTitleRightRelativeLayout;//title右边布局 protected ImageView mTitleLeftImageView;//title左边icon显示 protected Button mTitleLeftButton;//title左边文本显示 protected TextView mTitleMiddleTextView;//title中间文本显示 protected TextView mSubTitleTextView;//副标题文本显示 protected TextView mTitleRightTextView;//title右边文本显示 protected ImageView mTitleRightImageView;//title右边图片显示 protected ImageView mTitleRightTips;//title右边小红点提示
2,通过代码创建动态布局,省去了加载xml文件,则提高代码运行效率;
/** *设置一个布局容器,装载title_layout和传进来的layoutId 返回一个View视图 * @param layoutId * @return */ protected View initBar(int layoutId){ relativeLayout = new RelativeLayout(activity); mScrollView = new ScrollView(activity); RelativeLayout.LayoutParams rootLl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); relativeLayout.setLayoutParams(rootLl); mTitleView = activity.getLayoutInflater().inflate(R.layout.title_layout_activity,null); mTitleBgColor = (RelativeLayout) mTitleView.findViewById(R.id.title_bar_id); mTitleLeftRelativeLayout = (RelativeLayout) mTitleView.findViewById(R.id.left_relative_layout); mTitleMiddleRelativeLayout = (RelativeLayout) mTitleView.findViewById(R.id.mid_relative_layout); mTitleRightRelativeLayout = (RelativeLayout) mTitleView.findViewById(R.id.right_relative_layout); mTitleLeftImageView = (ImageView) mTitleView.findViewById(R.id.left_icon_title); mTitleLeftButton = (Button) mTitleView.findViewById(R.id.left_btn); mTitleMiddleTextView = (TextView) mTitleView.findViewById(R.id.middle_text); mSubTitleTextView = (TextView) mTitleView.findViewById(R.id.middle_sub_text); mTitleRightTextView = (TextView) mTitleView.findViewById(R.id.title_right_text); mTitleRightImageView = (ImageView) mTitleView.findViewById(R.id.title_right_image_view); mTitleRightTips = (ImageView) mTitleView.findViewById(R.id.right_new_info); mRootView = LayoutInflater.from(activity).inflate(layoutId,null);//activity布局文件 RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); rl.addRule(RelativeLayout.BELOW, R.id.title_bar_id); relativeLayout.addView(mTitleView); relativeLayout.setClipToPadding(false); relativeLayout.setFitsSystemWindows(true); relativeLayout.addView(mRootView); mRootView.setLayoutParams(rootLl); // RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) mRootView.getLayoutParams();//面板没有全屏 Log.e("qxf","mRootView.getWidth():"+mRootView.getWidth()+"mRootView.getHeight():"+mRootView.getHeight()+"mRootView.getMeasuredWidth():"+mRootView.getMeasuredWidth()); mTitleLeftButton.setOnClickListener(this); mTitleLeftRelativeLayout.setOnClickListener(this); mTitleRightRelativeLayout.setOnClickListener(this); mTitleMiddleRelativeLayout.setOnClickListener(this); return relativeLayout; }
3,注册监听事件
/** * 设置监听事件 * @param mTitleBuilderListener */ public void setTitleBuilderListener(TitleBuilderListener mTitleBuilderListener){ this.mListener = mTitleBuilderListener; } @Override public void onClick(View view) { if (mListener == null) { return; } if (view == mTitleLeftImageView|| view == mTitleLeftButton || view == mTitleLeftRelativeLayout){ mListener.onButtonClicked(TitleButton.LEFT); }else if (view == mTitleMiddleTextView || view == mTitleMiddleRelativeLayout){ mListener.onButtonClicked(TitleButton.MIDDLE); } else if(view == mTitleRightRelativeLayout || view == mTitleRightImageView || view == mTitleRightTextView){ mListener.onButtonClicked(TitleButton.RIGHT); } } public interface TitleBuilderListener { void onButtonClicked(TitleButton clicked); } }
二,在BaseActivty中集成
1、baseActivity是基类,集成过导航栏,其他activity不需要再次编写导航栏了,也不用include xml布局文件,可以统一的处理导航栏,来试试吧;
public abstract class BaseActivity extends Activity implements TitleBuilder.TitleBuilderListener { public TitleBuilder mTitleBuilder; private int TitleBarVisible; private boolean isChanageStatus = true; private boolean isFullScreen = false; private boolean isFirst = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); } @Override public void setContentView(int id) { this.TitleBarVisible = View.VISIBLE; mTitleBuilder = new TitleBuilder(this); this.setToolBarVisible(this.TitleBarVisible); super.setContentView(mTitleBuilder.initBar(id)); this.initToolBar(mTitleBuilder); this.setFullScreen(isFullScreen); mTitleBuilder.setTitleBuilderListener(this); setStatusBar(this.getTitleColor(),isChanageStatus); } @Override public void onButtonClicked(TitleBuilder.TitleButton clicked) { switch (clicked) { case LEFT: finish(); Toast.makeText(this, "this is back", Toast.LENGTH_SHORT).show(); break; } } public abstract void initToolBar(TitleBuilder mTitleBuilder); /** * 设置title是否可见 默认是可见状态 * * @param visible */ public void setToolBarVisible(int visible) { if (this.mTitleBuilder.mTitleView != null) { if (this.TitleBarVisible == visible) return; this.mTitleBuilder.mTitleView.setVisibility(visible); } this.TitleBarVisible = visible; } /** * 设置状态的属性 * * @param statusBar */ public void setStatusBar(int statusColor,boolean statusBar) { this.isChanageStatus = statusBar; if (statusBar) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; winParams.flags |= bits; win.setAttributes(winParams); } //透明状态栏 SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintColor(statusColor); //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); tintManager.setNavigationBarTintEnabled(true); //透明导航栏 // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } public void setFullScreen(boolean isFullScreen) { // if (isFullScreen) { // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // }else{ // //getWindow().setFlags(WindowManager.LayoutParams.F,WindowManager.LayoutParams.FLAG_FULLSCREEN); // } this.isFullScreen = isFullScreen; if (isFullScreen) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(lp); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.setToolBarVisible(View.GONE); } else { WindowManager.LayoutParams attr = getWindow().getAttributes(); attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setAttributes(attr); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); this.setToolBarVisible(View.VISIBLE); } } @Override protected void onResume() { this.setContentScreen(); super.onResume(); } private float getScreenHeight() { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); return dm.heightPixels; } private void setContentScreen() { int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); mTitleBuilder.mRootView.measure(widthSpec, heightSpec); mTitleBuilder.mTitleView.measure(widthSpec, heightSpec); mTitleBuilder.mRootView.getViewTreeObserver().addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { float height; if(isFullScreen){ height = mTitleBuilder.mRootView.getMeasuredHeight(); }else if (TitleBarVisible == View.GONE){ height = mTitleBuilder.mRootView.getMeasuredHeight() + getStatusBarHeight(); }else{ height = mTitleBuilder.mRootView.getMeasuredHeight() + mTitleBuilder.mTitleView.getMeasuredHeight() + getStatusBarHeight(); } Log.e("qxf", "height:" + height + "getScreenHeight():" + getScreenHeight() + "content" + mTitleBuilder.mRootView.getMeasuredHeight()); if (!isFirst) { if (height+1 > getScreenHeight()) { isFirst = true; Log.e("qxf", "height:" + height + "getScreenHeight():" + getScreenHeight()); ViewGroup viewParent = (ViewGroup) mTitleBuilder.mRootView.getParent(); Log.e("qxf", viewParent + "111111111"); if (viewParent != null) viewParent.removeView(mTitleBuilder.mRootView); ScrollView scrollView = new ScrollView(BaseActivity.this); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); rl.addRule(RelativeLayout.BELOW, R.id.title_bar_id); scrollView.setLayoutParams(rl); scrollView.setBackgroundColor(getResources().getColor(R.color.withe)); // viewParent.addView(mTitleBuilder.mTitleView); scrollView.addView(mTitleBuilder.mRootView); viewParent.addView(scrollView); } else { Log.e("qxf", "getScreenHeight():" + getScreenHeight() + "mTitleBuilder.mRootView.getHeight()" + height); } } return true; } }); } private float getStatusBarHeight() { Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); sbar = getResources().getDimensionPixelSize(x); } catch (Exception e1) { Log.e("qxf", "get status bar height fail" + e1); e1.printStackTrace(); } return sbar; }
测试效果图: