博主介绍:本人专注于Android/java/数据库/微信小程序技术领域的开发,以及有好几年的计算机毕业设计方面的实战开发经验和技术积累;尤其是在安卓(Android)的app的开发和微信小程序的开发,很是熟悉和了解;本人也是多年的Android开发人员;希望我发布的此篇文件可以帮助到您;
🍅文章末尾获取源码下载方式🍅
实现效果

一、自定义view
public class GridRadioGroup extends RadioGroup {
//显示的列数
private int columnNum = 2;//默认2列
public GridRadioGroup(Context context) {
this(context, null);
}
public GridRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GridRadioGroup, 0, 0);
try {
columnNum = ta.getInt(R.styleable.GridRadioGroup_columnNum, 2);
} finally {
ta.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int itemWidth = (widthSize - (columnNum + 1)) / columnNum;
int childCount = getChildCount();
int itemHeight = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
child.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY), MeasureSpec.UNSPECIFIED);
itemHeight = child.getMeasuredHeight();
}
int rows = childCount % columnNum == 0 ? childCount / columnNum : childCount / columnNum + 1;
int heightSize = rows * itemHeight + (rows + 1);
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 % columnNum;
int cl, ct, cr, cb;
if (i >= columnNum - 1 && yu == 0) {
rows++;
}
cl = (yu + 1) + yu * width;
ct = (rows + 1) + rows * height;
cr = (yu + 1) + (yu + 1) * width;
cb = (rows + 1) + (rows + 1) * height;
view.layout(cl, ct, cr, cb);
}
}
/**
* 设置列数
* @param columnNum
*/
public void setColumnNum(int columnNum) {
this.columnNum = columnNum;
}
二、attrs.xml配置
在项目res--values目录下attrs.xml中添加一下如下属性
<declare-styleable name="GridRadioGroup">
<attr name="columnNum" format="integer" />
</declare-styleable>
二、在xml布局文件
<com.clientBase.view.GridRadioGroup
android:id="@+id/mrbChoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
app:columnNum="2"
>
<RadioButton
android:id="@+id/mrb1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:button="@drawable/choice_contact_bg"
android:checked="true"
android:padding="8dp"
android:text="好学好喝"
android:textColor="#666"
android:textSize="14dp" />
<RadioButton
android:id="@+id/mrb2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:button="@drawable/choice_contact_bg"
android:padding="8dp"
android:text="简单易懂 "
android:textColor="#666"
android:textSize="14dp" />
<RadioButton
android:id="@+id/mrb3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:button="@drawable/choice_contact_bg"
android:padding="8dp"
android:text="一般 "
android:textColor="#666"
android:textSize="14dp" />
<RadioButton
android:id="@+id/mrb4"
android:layout_width="match_parent"
android:layout_height="40dp"
android:button="@drawable/choice_contact_bg"
android:padding="8dp"
android:text="难学不好喝 "
android:textColor="#666"
android:textSize="14dp" />
</com.clientBase.view.GridRadioGroup>
该文章介绍了如何创建一个自定义的GridRadioGroup视图,它扩展了RadioGroup,支持自定义列数。作者分享了类的实现、XML布局配置以及如何设置列数的方法。
1023

被折叠的 条评论
为什么被折叠?



