Android自定义实现多行多列RadioGroup

该文章介绍了如何创建一个自定义的GridRadioGroup视图,它扩展了RadioGroup,支持自定义列数。作者分享了类的实现、XML布局配置以及如何设置列数的方法。

博主介绍:本人专注于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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android毕业设计源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值