FlowRadioGroup给RadioGroup的子控件随意排版

RadioGroup默认将子控件排版只有横排和纵排,如果想让子控件按自己的要求排版怎么做呢?在网上搜了FlowRadioGroup


package com.example.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;

/**
 * 流式布局的RadioGroup
 */
public class FlowRadioGroup extends RadioGroup {

	public FlowRadioGroup(Context context) {
		super(context);
	}

	public FlowRadioGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childCount = getChildCount();
		int x = 0;
		int y = 0;
		int row = 0;

		for (int index = 0; index < childCount; index++) {
			final View child = getChildAt(index);
			if (child.getVisibility() != View.GONE) {
				child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
				// 此处增加onlayout中的换行判断,用于计算所需的高度
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				if (x > maxWidth) {
					x = width;
					row++;
					y = row * height + height;
				}
			}
		}
		// 设置容器所需的宽度和高度
		setMeasuredDimension(maxWidth, y);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int childCount = getChildCount();
		int maxWidth = r - l;
		int x = 0;
		int y = 0;
		int row = 0;
		for (int i = 0; i < childCount; i++) {
			final View child = this.getChildAt(i);
			if (child.getVisibility() != View.GONE) {
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				x += width;
				y = row * height + height;
				if (x > maxWidth) {
					x = width;
					row++;
					y = row * height + height;
				}
				child.layout(x - width, y - height, x, y);
			}
		}
	}
}

frm.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.test.FlowRadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1.按钮一"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2.按钮二"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3.按钮三(不换行)"
            android:textSize="25sp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/t1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4.天数"
                android:textSize="25sp" >
            </TextView>

            <EditText
                android:id="@+id/e1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/editview_bg_selector"
                android:ems="6"
                android:text="1" >
            </EditText>

            <TextView
                android:id="@+id/t2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="天"
                android:textSize="25sp" >
            </TextView>
        </LinearLayout>

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5.按钮四(内容太长,换行)"
            android:textSize="25sp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/t12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="6.本周工作的天数"
                android:textSize="25sp" >
            </TextView>

            <EditText
                android:id="@+id/e2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/editview_bg_selector"
                android:ems="6"
                android:text="12" >
            </EditText>

            <TextView
                android:id="@+id/t22"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="天"
                android:textSize="25sp" >
            </TextView>
        </LinearLayout>

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7.按钮五"
            android:textSize="25sp" />
    </com.example.test.FlowRadioGroup>

</LinearLayout>

如果想要在RadioButton后面跟非RadioButton类型的,怎么做呢,我对FlowRadioGroup进行改写

MyRadioGroup.java

package com.example.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/*
 *流式布局的RadioGroup
 */
public class FlowRadioGroup extends RadioGroup {

	public FlowRadioGroup(Context context) {
		super(context);
	}

	public FlowRadioGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childCount = getChildCount();
		int x = 0;
		int y = 0;
		int row = 0;

		for (int index = 0; index < childCount; index++) {
			final View child = getChildAt(index);
			if (child.getVisibility() != View.GONE) {
				child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				// 第二行开始的RadioButton换行
				if (index > 0 && child instanceof RadioButton) {
					x = width;
					row++;
				} else {
					x += width;
					if (x > maxWidth) {//非RadioButton类型,如果和上个RadioButton长度加起来超过屏幕宽度就换行
						x = width;
						row++;
					}
				}
				y = row * height + height;
			}
		}
		// 设置容器所需的宽度和高度
		setMeasuredDimension(maxWidth, y);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int childCount = getChildCount();
		int maxWidth = r - l;
		int x = 0;
		int y = 0;
		int row = 0;
		for (int index = 0; index < childCount; index++) {
			final View child = this.getChildAt(index);
			if (child.getVisibility() != View.GONE) {
				int width = child.getMeasuredWidth();
				int height = child.getMeasuredHeight();
				if (index > 0 && child instanceof RadioButton) {
					x = width;
					row++;
				} else {
					x += width;
					if(!(child instanceof RadioButton)){
						child.setPadding(30, child.getPaddingTop(), child.getPaddingRight(), child.getPaddingBottom());
					}
					if (x > maxWidth) {
						x = width;
						row++;
					}
				}
				y = row * height + height;
				child.layout(x - width, y - height, x, y);
			}
		}
	}
}
布局文件和上方的frg.xml一样,效果如图:

第三和第四在一行,第五和第六宽度加起来超过屏幕宽度,就让第六换行了。


让自定义的RadioGroup继承RadioGroup类,用法和RadioGroup一样,只是将子控件重新排列了一下,不影响使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值