FlowRadioGroup给RadioGroup的子控件随意排版

来源:互联网 发布:java建网站 编辑:程序博客网 时间:2024/05/22 00:12

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);}@Overrideprotected 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);}@Overrideprotected 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);}@Overrideprotected 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);}@Overrideprotected 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一样,只是将子控件重新排列了一下,不影响使用

0 0
原创粉丝点击