abstract
In the current development project, we find that there are not only "return icon - Title text" type of ordinary toolbar, but also "return icon - title text - menu icon" and "text - title text - text" type. That is to say, the left and right sides of Toolbar may be text or icon, and the middle. Usually they are titles, so think about encapsulating toolbar to suit this need.
text
The simple thing to do is to customize a layout, then put an ImageView and a TextView on the left, a TextView in the middle, and an ImageView and a TextView on the right. Then expose some methods for users to define text or icons. When using it, if there are text on both sides, instead of icons, it is necessary to define the click listen event and then set the text (the original text is for click, so the click listen event is set in the setText method, so the listen event can not be empty, so it is necessary to define the listen event before setting the text content). There are many ways, the code here is very simple, there is not much to talk about, but there are also places to optimize, to be free in the future to optimize again==!!
public class ToolBarHelper implements Toolbar.OnMenuItemClickListener, View.OnClickListener { private FrameLayout mRootLayout; private Toolbar mToolbar; private View mTargetView; private FrameLayout mTargetLayout ; public LayoutInflater mInflater; private boolean hasArrow; View view ; TextView tvLeft ; TextView tvRight ; ImageView ivIcon ; OnToolbarRightItemClickListener onToolbarRightItemClickListener; OnToolbarLeftItemClickListener onToolbarLeftItemClickListener ; RelativeLayout rightLayout; public ToolBarHelper(Context context, LayoutInflater inflater, int layoutID,boolean hasArrow) { this.mInflater = inflater; this.mTargetView = mInflater.inflate(layoutID, null); initView(context,hasArrow); } public ToolBarHelper(Context context, LayoutInflater inflater, View view,boolean hasArrow) { this.mInflater = inflater; this.mTargetView = view; initView(context,hasArrow); } private void initView(Context context,boolean hasArrow) { FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); FrameLayout.LayoutParams layoutParamMarginTop = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); layoutParamMarginTop.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, context.getResources().getDisplayMetrics()); mRootLayout = new FrameLayout(context); mRootLayout.setLayoutParams(layoutParam); mTargetLayout = new FrameLayout(context) ; mTargetLayout.setLayoutParams(layoutParamMarginTop); mTargetLayout.addView(mTargetView); mRootLayout.addView(mTargetLayout); this.hasArrow = hasArrow; view = mInflater.inflate(R.layout.tool_bar_layout, mRootLayout); tvLeft = (TextView) view.findViewById(R.id.toolbar_remark); tvRight = (TextView) view.findViewById(R.id.toolbar_menu); ivIcon = (ImageView) view.findViewById(R.id.img_icon) ; mToolbar = (Toolbar) view.findViewById(R.id.id_tool_bar); rightLayout = (RelativeLayout) view.findViewById(R.id.toolbar_right_layout); if (hasArrow){ mToolbar.setNavigationIcon(R.mipmap.fanhui); } mToolbar.setNavigationOnClickListener(this); mToolbar.setOnMenuItemClickListener(this); } public void setLeftText(String text) { tvLeft.setText(text); tvLeft.setVisibility(View.VISIBLE); setLeftClick(); } public TextView getLeftText() { if (tvLeft == null) { tvLeft = (TextView) view.findViewById(R.id.toolbar_remark) ; } return tvLeft; } public TextView getRigtText() { if (tvRight == null) { tvRight = (TextView) view.findViewById(R.id.toolbar_menu) ; } return tvRight; } public ImageView getRigtIcon() { if (ivIcon == null) { ivIcon = (ImageView) view.findViewById(R.id.img_icon) ; } return ivIcon; } public void setLeftText(int text) { tvLeft.setText(text); tvLeft.setVisibility(View.VISIBLE); setLeftClick(); } public void setRightText(String right) { tvRight.setText(right); tvRight.setVisibility(View.VISIBLE); setRightClick(); } private void setLeftClick() { if (this.onToolbarLeftItemClickListener != null) { tvLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onToolbarLeftItemClickListener.onLeftItemClick(); } }); } } private void setRightClick() { if (this.onToolbarRightItemClickListener != null) { rightLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onToolbarRightItemClickListener.onRightItemClick(); } }); } } public void setRightText(int right) { tvRight.setText(right); tvRight.setVisibility(View.VISIBLE); setRightClick(); } public void setTitle(String title) { TextView tvTitle = (TextView) view.findViewById(R.id.toolbar_title); tvTitle.setText(title); tvTitle.setVisibility(View.VISIBLE); } public void setTitle(int title) { TextView tvTitle = (TextView) view.findViewById(R.id.toolbar_title); tvTitle.setText(title); tvTitle.setVisibility(View.VISIBLE); } public void setRightIcon(int icon) { ivIcon.setVisibility(View.VISIBLE); ivIcon.setImageResource(icon); setRightIconClick(); } public void setRightIcon(Drawable icon) { ivIcon.setVisibility(View.VISIBLE); ivIcon.setImageDrawable(icon); setRightIconClick(); } private void setRightIconClick() { if (this.onToolbarRightItemClickListener != null) { rightLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onToolbarRightItemClickListener.onRightItemClick(); } }); } } public void setOnToolbarRightItemClickListener(OnToolbarRightItemClickListener onToolbarRightItemClickListener) { this.onToolbarRightItemClickListener = onToolbarRightItemClickListener; } public void setOnToolbarLeftItemClickListener(OnToolbarLeftItemClickListener onToolbarLeftItemClickListener) { this.onToolbarLeftItemClickListener = onToolbarLeftItemClickListener; } @Override public boolean onMenuItemClick(MenuItem item) { if (mOnToolbarActionListener != null) { mOnToolbarActionListener.onToolBarMenuItemClick(item); } return true; } @Override public void onClick(View v) { if (mOnToolbarActionListener != null) { mOnToolbarActionListener.onToolBerNavigationClick(v); } } public interface OnToolbarActionListener { void onToolBerNavigationClick(View view); void onToolBarMenuItemClick(MenuItem item); } private OnToolbarActionListener mOnToolbarActionListener; public void setOnToolbarActionListener(OnToolbarActionListener l) { this.mOnToolbarActionListener = l; } public FrameLayout getRootLayout() { return mRootLayout; } public View getTargetView() { return mTargetView; } public FrameLayout getmTargetLayout() { return mTargetLayout; } public Toolbar getToolbar() { return mToolbar; } public interface OnToolbarRightItemClickListener { void onRightItemClick(); } public interface OnToolbarLeftItemClickListener { void onLeftItemClick() ; } }
It exposes the methods of setting left and right text, right icon and so on. If you want to customize other properties of text and icon, you can also take these controls and assign corresponding values directly. Let's not go into details.