摘要
现在开发的项目中,发现需求里面,既有“返回图标-标题文字”类型的普通toolbar,也有“返回图标-标题文字-菜单图标”和“文字-标题文字-文字”这种类型的,也就是说,Toolbar的左边和右边都有可能是文字或者图标,而中间一般都是标题,所以寻思着封装下toolbar,使之能适应这种需求。
正文
要做的东西很简单,就是自定义一个布局,然后左边放一个ImageView和一个TextView,中间放一个TextView,右边同样放一个ImageView和一个TextView。然后暴露一些方法给使用者去定义文字或者图标咯。使用时,如果是两边有文字,而不是图标,则需先定义点击监听事件,再设置文本(本来文字就是为了点击的,所以在setText方法里给设置了click监听事件,故而监听事件不能为空,所以要先定义监听事件再设置文本内容),实现的方式有很多,这边的代码也很简单,没有太多需要讲的地方,不过要优化的地方也有,待日后有空了优化一遍=_=!!
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() ;
}
}
里面暴露了设置左右文字,右边图标等方法,如果想自定义文字和图标其他属性,也可以取到这些控件,然后直接赋相应值。就不赘述了。