自定义 多列 RadioGroup

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>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值