一、代码中在RadioGroup中动态添加RadioButton时的自动换行实现
使用自定义RadioGroup,代码如下:
public class CustomRadioGroup extends RadioGroup {
private List<RowView> rowViews;//存放每行radioBotton的集合
private int horizontalSpacing =20;//默认水平间距
private int verticalSpacing = 10;//默认垂直间距
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<>();
}
//设置水平间距(单位dp)
public void setHorizontalSpacing(int horizontalSpacing_dp) {
this.horizontalSpacing = dip2px(mContext, horizontalSpacing_dp);
}
//设置垂直间距(单位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() -