Since the next version of the project added the commodity selection requirement, it was necessary to add a set of radio label stream. Considering that there were not many label options, it was better to use a custom RadioGroup.
The emphasis is on onMeasure method to calculate the broadband of each line for branch, onLayout method to set the position of each RadioButton.
Look at the renderings first
Paste_Image.png
Customradiogroup.java code
package com.dengxiao.customradiogrouptag; import android.content.Context; import android.util.AttributeSet; import android.util.SparseArray; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; /** * Created by Dengxiao on 2016/12/2. */ public class CustomRadioGroup extends RadioGroup { private List rowViews; Private int horizontalSpacing =20; // Default spacing private int verticalSpacing = 10; // Default vertical spacing private Context mContext; private OnclickListener listener; public void setListener(OnclickListener listener) { this.listener = listener; } public CustomRadioGroup(Context context) { this(context, null); } public CustomRadioGroup(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; rowViews = new ArrayList<>(); } public void setHorizontalSpacing(int horizontalSpacing_dp) {this.horizontalSpacing = dip2px(mContext, horizontalSpacing_dp); } public void setVerticalSpacing(int verticalSpacing_dp) {this.verticalSpacing = dip2px(mContext, verticalSpacing_dp); ; } public int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; Return (int) (dipValue * scale + 0.5f); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); rowViews.clear(); Paddingleft/paddingright int width = MeasureSpec. GetSize (widthMeasureSpec); Int nopaddingWidth = width-getPaddingLeft () -getpaddingright (); RowView rowView = null; For (int I = 0; i < getChildCount(); i++) { View childView = getChildAt(i); Childview. measure(0, 0); If (rowView == null) {rowView = new rowView (); } if (rowView.getrowviews ().size() == 0) {rowView.addchidView (childView); } else if (rowView.getRowWidth() + horizontalSpacing + childView.getMeasuredWidth() > nopaddingWidth) { Rowviews.add (rowView); RowView = new rowView (); // Create a new row, save the current view rowView.addchidView (childView); } else { rowView.addChidView(childView); } // If the current childView is the last child of the childView, If (I == getChildCount() -1) {rowviews.add (rowView); Int heght = getPaddingTop() + getPaddingBottom(); // add padding for (int I = 0; i < rowViews.size(); i++) { heght += rowViews.get(i).getRowHeight(); } heght += (rowviews.size () -1) * verticalSpacing; // Add measuredDimension (width, heght); If (getChildCount() == 0) {setMeasuredDimension(0, 0); }} @override protected void onLayout(Boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); for (int i = 0; i < rowViews.size(); i++) { RowView rowView = rowViews.get(i); If (I > 0) {paddingTop += rowviews.get (I -); paddingTop += rowviews.get (I -) 1).getRowHeight() + verticalSpacing; } List viewList = rowView.getRowViews(); For (int j = 0; j < viewList.size(); j++) { View childView = viewList.get(j); If (j == 0) {childView.layout(paddingLeft, paddingTop, paddingLeft); paddingLeft + childView.getMeasuredWidth(), paddingTop + childView.getMeasuredHeight()); } else {right View preView = viewList.get(j-1);} else {right View preView = viewList.get(j-1); View int left = preView. GetRight () + horizontalSpacing; A VIew right before / / + childView. The horizontal spacing layout (left, preView getTop (), left + childView. GetMeasuredWidth (), preView. GetBottom ()); } } } } public interface OnclickListener{ void OnText(String text); } class RowView { private List lineViews; View private int rowWidth; Private int rowHeight; Public RowView() {lineViews = new ArrayList<>(); } public List getRowViews() { return lineViews; } public int getRowWidth() { return rowWidth; } public int getRowHeight() { return rowHeight; } public void addChidView(view view) {if (! lineViews.contains(view)) { ((RadioButton)view).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(listener! =null){ listener.OnText(((RadioButton)v).getText().toString().trim()); }}}); If (lineviews.size () == 0) {// If (lineviews.size () == 0) {rowWidth = view.getMeasuredWidth(); } else {// not the first time, need to add horizontalSpacing rowWidth += view.getmeasuredwidth () + horizontalSpacing; } rowHeight = math.max (view.getMeasuredHeight(), rowHeight); lineViews.add(view); }}}}Copy the code
How to use activities
CustomRadioGroup customRadioGroup1 = (CustomRadioGroup) findViewById(R.id.customRadioGroup); SetSpacing (customRadioGroup1, 12, 8); for (int i = 0; i < str1.length; i++) { RadioButton radioButton = (RadioButton) this.getLayoutInflater().inflate(R.layout.radiobutton_addcart, null); radioButton.setText(str1[i]); customRadioGroup1.addView(radioButton); }customRadioGroup1.setListener(new CustomRadioGroup.OnclickListener() { @Override public void OnText(String text) { text1 = text; mTextView.setText(text1 + text2 + text3); }});Copy the code