第一次写博客,写了一个自定义的custombarbar控件,
先上效果吧
实现非常简单如下
private void bindData() { barbar1.addMiddleViewExist(barbar1.getTextView("标题1")); barbar2.addMiddleViewExist(barbar2.getTextView("标题2")); barbar2.addLeftView(barbar2.getImageView(R.mipmap.back)); barbar3.addMiddleViewExist(barbar3.getTextView("标题3")); barbar3.addLeftView(barbar3.getImageView(R.mipmap.back)); barbar3.addLeftView(barbar3.getImageView(R.mipmap.back)); barbar4.addLeftView(barbar4.getImageView(R.mipmap.back)); barbar4.addMiddleViewExist(barbar4.getTextView("标题4")); barbar4.addRightView(barbar4.getTextView("右1")); barbar4.addRightView(barbar4.getTextView("右2")); barbar5.addMiddleViewExist(barbar5.getTextView("标题5")); barbar5.addLeftView(barbar5.getTextView("左1")); barbar5.addLeftView(barbar5.getTextView("左2")); barbar5.addRightView(barbar5.getImageView(R.mipmap.add)); barbar5.addRightView(barbar5.getTextView("右2")); barbar5.addRightView(barbar5.getTextView("右3")); barbar6.addMiddleViewExist(barbar6.getTextView("标题6")); barbar6.addLeftView(barbar6.getTextView("左1")); barbar6.addLeftView(barbar6.getTextView("左2")); barbar6.addLeftView(barbar6.getTextView("左3")); barbar6.addRightView(barbar6.getImageView(R.mipmap.add)); barbar6.addRightView(barbar6.getTextView("右2")); barbar6.addRightView(barbar6.getTextView("右3")); barbar6.addRightView(barbar6.getTextView("右4")); }
主要是动态控制显示,可扩展性强
上控件代码........
public class CustomBarbar extends RelativeLayout { private ArrayList<ViewItem> views = new ArrayList<>(0); private int gap = 0; public CustomBarbar(Context context) { super(context); } public CustomBarbar(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public CustomBarbar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomBarbar); gap = a.getDimensionPixelSize(R.styleable.CustomBarbar_gap, 10); a.recycle(); } public View addLeftView(View view) { return addCustomView(view, Model.LEFT); } public View addRightView(View view) { return addCustomView(view, Model.RIGHT); } public View addMiddleViewExist(View view) { return addCustomView(view, Model.MIDDLE); } private View addCustomView(View view, Model model) { LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); View lastViewByModel = null; int rule = ALIGN_PARENT_LEFT; switch (model) { case LEFT: rule = ALIGN_PARENT_LEFT; break; case RIGHT: rule = ALIGN_PARENT_RIGHT; break; case MIDDLE: rule = CENTER_IN_PARENT; break; } for (ViewItem item : views) { if (item.model == model) { lastViewByModel = item.view; } } //中间只允许放一个 if (model == Model.MIDDLE && lastViewByModel != null) { removeView(lastViewByModel); lastViewByModel = null; } if (lastViewByModel == null) { lp.addRule(rule); } else { switch (model) { case LEFT: lp.leftMargin = gap; lp.addRule(RIGHT_OF, lastViewByModel.getId()); break; case RIGHT: lp.rightMargin = gap; lp.addRule(LEFT_OF, lastViewByModel.getId()); break; case MIDDLE: break; } } lp.addRule(CENTER_VERTICAL); addView(view, lp); views.add(new ViewItem(view, model)); view.setId(views.size()); return view; } public TextView getTextView(String text) { TextView textView = new TextView(getContext()); textView.setText(text); return textView; } public ImageView getImageView(int resId) { ImageView imageView = new ImageView(getContext()); if (resId != NO_ID) { imageView.setImageResource(resId); } return imageView; } private class ViewItem { public View view; public Model model = Model.LEFT; public ViewItem(View view, Model model) { this.view = view; this.model = model; } } public enum Model { LEFT, MIDDLE, RIGHT } }
addLeftView(View)
添加左边控件,按先后顺序排列,如下图
addRightView(View)
添加右边控件,按先后顺序排列,如下图
addMiddleViewExist(View)
添加中间控件,只允许同时存在一个
方法如其名,代码逻辑也很简单,希望大家多多提意见
我在控件内定义了一个getImageView和getTextView主要是这2个使用频率较高,添加其他的控件就可以自行初始化对应的控件
以前主要是做手机系统研发,最近开始写app,所以自己会写一些通用的东西
github地址:https://github.com/ff525257/custombarbar