I wrote a blog for the first time and wrote a custom custombarbar control.
Let's get the effect first
The implementation is very simple as follows
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")); }
Mainly dynamic control display, strong scalability
Upper control code........
/** * addLeftView adds the left control, arranged in order * addRightView adds the controls on the right, arranged in order * addMiddleViewExist adds an intermediate control, only one is allowed at the same time * * @author fmh */ public class CustomBarbar extends RelativeLayout { private ArrayList<ViewItem> views = new ArrayList<>(0); /** * Clearance */ 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; } } //Only one is allowed in the middle 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; } /** * Get textview * * @param text * @return */ public TextView getTextView(String text) { TextView textView = new TextView(getContext()); textView.setText(text); return textView; } /** * Get ImageView * * @param resId * @return */ 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)
Add the controls on the left, arranged in order, as shown below
addRightView(View)
Add the controls on the right, arranged in order, as shown below
addMiddleViewExist(View)
Add middle control, only allow one at the same time
The method is as the name suggests, and the code logic is also very simple. I hope you can provide more opinions
I defined a getImageView and getTextView in the control. These two are used more frequently. You can initialize the corresponding control by adding other controls.
In the past, I mainly did mobile phone system research and development. Recently I started to write apps, so I will write some general things.
github address: https://github.com/ff525257/custombarbar