RadioGroup 继承 LinearLayout , 这个布局的特点,子布局线性排列,要么横要么竖。 现在需要RadioGroup 分成多列,于是需要自定义RadioGroup ,重新测量、布局,进而达到下图效果
父布局是自定义的RadioGroup子布局是SDK默认的RadioButton
自定义的 radiogroup 下面代码就是
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;
public class GridRadioGroup extends RadioGroup {
//子布局RadioButton 列数
private int column = 2;
//文字
private int textPaddingTB = 30;
private int widthmargin = 0;
private int heighmargin = 18;
private int itemWidth;
public GridRadioGroup(Context context) {
this(context, null);
}
public GridRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
itemWidth = (widthSize - (column + 1) * widthmargin) / column;
int childCount = getChildCount();
int itemHeight = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
child.setPadding(0, textPaddingTB, 0, textPaddingTB);
child.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY),
MeasureSpec.UNSPECIFIED);
itemHeight = child.getMeasuredHeight();
}
int rows = childCount % column == 0 ? childCount / column : childCount / column + 1;
int heightSize = rows * itemHeight + (rows + 1) * heighmargin;
setMeasuredDimension(widthMeasureSpec, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int rows = 0;
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
int yu = i % column;
int cl, ct, cr, cb;
if (i >= column - 1 && yu == 0) {
rows++;
}
cl = widthmargin * (yu + 1) + yu * width;
ct = heighmargin * (rows + 1) + rows * height;
cr = widthmargin * (yu + 1) + (yu + 1) * width;
cb = heighmargin * (rows + 1) + (rows + 1) * height;
view.layout(cl, ct, cr, cb);
}
}
}
布局xml代码:
<com.xxx.widget.GridRadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="废金属"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="再生塑料"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
<RadioButton
android:id="@+id/radiobutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="废纸"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
<RadioButton
android:id="@+id/radiobutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="铜厂、铜材厂"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
<RadioButton
android:id="@+id/radiobutton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调剂商、贸易商"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
<RadioButton
android:id="@+id/radiobutton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="钢厂、钢铁冶金"
android:textColor="@color/col_333333"
android:textSize="@dimen/sp_14" />
</com.xxx.widget.GridRadioGroup>