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
- FlowRadioGroup给RadioGroup的子控件随意排版
- Android-->FlowRadioGroup(流式布局RadioGroup, 自定义View的简单使用)
- 随意拖拽的控件
- RadioGroup 的check(id)和他的子控件radiobutton 的setcheck(boolean)的区别
- Android RadioGroup控件的使用
- RadioGroup单选按钮排版
- 随意拖动的picturebox服务器控件源码
- 服务器控件之间不能随意的嵌套
- 服务器控件之间不能随意的嵌套
- 控件RadioGroup、RadioButton和CheckBox的使用
- RadioGroup,RadioButton CheckBox控件的使用
- .定义你写的RadioGroup控件
- 子页面赋值给父页面的控件方法
- 给asp:DropDownList控件的ItemList子项目加空格
- LaTeX排版指定行列的子图
- Android ListView的子项的随意拖动效果
- Android ListView的子项的随意拖动效果
- 关于LinnerLayout动态添加的控件 并且给子控件动态设置Id 、点击的问题
- ZOJ 3863 Paths on the Tree 树分治
- 网站”改“的好,排名升得快
- leetcode 4 Median of Two Sorted Arrays
- EAS小贷系统(财务业务一体化)
- Javascript和JQuery中常用的随机数产生函数
- FlowRadioGroup给RadioGroup的子控件随意排版
- 自动化运维工具Ansible详细部署
- 第六周 阅读程序(5)
- C++中的locale设置 (即系统区域设置)
- AngularJS的应用(2)
- 《open公开课》嵌入式/Android
- Linux----top命令
- 12.FragmentStatePagerAdapter中的getItem方法没有被调用,导致Fragment界面在ViewPager中无法显示。
- hive