public class LlkjToolBar extends Toolbar { private View view; private TextView toolbarTitle; private TextView amRightTv; private TextView amLeftTv; public static final int isNull = -1; //布局 private LayoutInflater mInflater; public LlkjToolBar(Context context) { this(context, null); } public LlkjToolBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LlkjToolBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //初始化函数 initView(); setContentInsetsRelative(0, 0); //设置默认销毁当前页面 if (amLeftTv != null) amLeftTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); } private void initView() { if (view == null) { //初始化 mInflater = LayoutInflater.from(getContext()); //添加布局文件 view = mInflater.inflate(R.layout.toolbar, null); //绑定控件 toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title); amRightTv = (TextView) view.findViewById(R.id.am_right_tv); amLeftTv = (TextView) view.findViewById(R.id.am_left_tv); LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL); addView(view, layoutParams); } } //判断是否有头部 public void initToolBar() { setTitle(""); setEnabled(true); } public void setToolBar(int title, boolean isshowL, int wzidL, int pidL, boolean isshowR, int wzidR, int pidR) { initToolBar(); setTitleText(title); if (isshowL) { if (wzidL != isNull) { setLeftText(wzidL); } if (pidL != isNull) { setLeftImg(pidL); } } if (isshowR) { if (wzidR != isNull) { setRightText(wzidR); } if (pidR != -1) { setRightImg(pidR); } } } public void setToolBar(String title, boolean isShowL, String wzidL, int pidL, boolean isShowR, String wzidR, int pidR) { initToolBar(); setTitleText(title); if (isShowL) { if (!TextUtils.isEmpty(wzidL)) { setLeftText(wzidL); } if (pidL != isNull) { setLeftImg(pidL); } }else{ setLeftButtonOnClickLinster(null); } if (isShowR) { if (!TextUtils.isEmpty(wzidR)) { setRightText(wzidR); } if (pidR != isNull) { setRightImg(pidR); } } } //设置右侧按钮监听事件 public void setRightButtonOnClickLinster(OnClickListener linster) { if (amRightTv != null) amRightTv.setOnClickListener(linster); } //设置左侧按钮监听事件 public void setLeftButtonOnClickLinster(OnClickListener linster) { if (amLeftTv != null) amLeftTv.setOnClickListener(linster); } public void setTitleText(String text) { if (toolbarTitle != null) toolbarTitle.setText(text); } public void setTitleTextColor(@ColorRes int color) { if (toolbarTitle != null) toolbarTitle.setTextColor(ResourceUtil.getColor(getContext(),color)); } public void setTitleText(@StringRes int textId) { if (toolbarTitle != null) toolbarTitle.setText(textId); } public void setTitleBg(@ColorRes int color){ if(view!=null&&view.findViewById(R.id.toolbarbg)!=null){ view.findViewById(R.id.toolbarbg).setBackgroundColor(ResourceUtil.getColor(getContext(),color)); } } //设置左边图片 public void setLeftImg(@DrawableRes int imgId) { if (amLeftTv != null) amLeftTv.setCompoundDrawablesWithIntrinsicBounds(imgId, 0, 0, 0); } //设置左边文字 public void setLeftText(String text) { if (amLeftTv != null) amLeftTv.setText(text); } //设置左边字体颜色 public void setLeftTextColor(@ColorRes int color) { if (amLeftTv != null) amLeftTv.setTextColor(ResourceUtil.getColor(getContext(),color)); } public void setLeftText(@StringRes int textId) { if (amLeftTv != null) amLeftTv.setText(textId); } //设置右边文字 public void setRightText(String text) { if (amRightTv != null) amRightTv.setText(text); } //设置右边字体颜色 public void setRightTextColor(@ColorRes int color) { if (amRightTv != null) amRightTv.setTextColor(ResourceUtil.getColor(getContext(),color)); } public void setRightText(@StringRes int textId) { if (amRightTv != null) amRightTv.setText(textId); } //设置右边图片 public void setRightImg(@DrawableRes int imgId) { if (amRightTv != null) amRightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, imgId, 0); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.bloveoa.widget.LlkjToolBar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/toolBarheight" android:minHeight="?attr/actionBarSize" /> </LinearLayout>
public void setToolbar(Toolbar mToolbar) { setSupportActionBar(mToolbar); if (getSupportActionBar() != null) { //隐藏标题栏 getSupportActionBar().setDisplayShowTitleEnabled(false); } }
2.使用方式
、布局引入
<include layout="@layout/toolbar1" />
setToolbar(mToolbar); mToolbar.setToolBar(R.string.sign, true, LlkjToolBar.isNull, R.drawable.ic_back, true, LlkjToolBar.isNull, LlkjToolBar.isNull);
3. R.layout.toolbar
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toolbarbg" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/paddingtop" android:background="@color/title_bg" > <TextView android:id="@+id/am_left_tv" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:background="?attr/actionBarItemBackground" android:clickable="true" android:gravity="left|center_vertical" android:minWidth="@dimen/dp_50" android:paddingLeft="15dp" android:textColor="@color/colorPrimary" tools:text="edit"/> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="我的App" android:textColor="@android:color/white" android:textSize="18sp"/> <TextView android:id="@+id/am_right_tv" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="end" android:background="?attr/actionBarItemBackground" android:clickable="true" android:gravity="right|center_vertical" android:minWidth="@dimen/dp_50" android:paddingRight="@dimen/dp_15" android:textColor="@color/white" tools:text="edit"/> </FrameLayout>