33
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package cn.shangyu.gdxzExpert.widget;
|
||||
|
||||
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 = 0;
|
||||
|
||||
private int widthmargin = 0;
|
||||
private int heighmargin = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user