RadioButton实现自动换行的效果
来源:互联网 发布:单词朗读软件 编辑:程序博客网 时间:2024/06/05 02:35
android中单选按钮没有自动换行的效果,这里继承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); //获取Group中的Child数量 int childCount = getChildCount(); //设置Group的左边距,下面也会使用x计算每行所占的宽度 int x = 0; //设置Group的上边距,下面也会使用y计算Group所占的高度 int y = 30; //设置Group的行数 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); //重新计算child的宽高 int width = child.getMeasuredWidth(); int height = child.getMeasuredHeight(); //添加到X中,(width+10) 设置child左边距 x += (width + 10); //行数*child高度+这次child高度=现在Group的高度,(height + 10)设置child上边距 y = row * (height + 10) + (height + 10); //当前行宽X大于Group的最大宽度时,进行换行 if (x > maxWidth) { //当index不为0时,进行row++,防止FirstChild出现大于maxWidth时,提前进行row++ if (index != 0) row++; //child的width大于maxWidth时,重新设置child的width为最大宽度 if (width >= maxWidth) { width = maxWidth - 30; } //重新设置当前X x = (width + 20); //重新设置现在Group的高度 y = row * (height + 10) + (height + 10); } } } // 设置容器所需的宽度和高度 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 = 10; 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 + 10); y = row * (height + 10) + (height + 10); if (x > maxWidth) { if (i != 0) row++; if (width >= maxWidth) { width = maxWidth - 30; } x = (width + 20); y = row * (height + 10) + (height + 10); } child.layout(x - width, y - height, x, y); } } }}
源码
0 0
- RadioButton实现自动换行的效果
- Android:实现类似RadioButton自动换行及单选效果
- 可自动换行的radiobutton
- radiobutton超出本行后自动换行实现
- 自动换行RadioButton
- asp.net 中Label 实现自动换行的效果!
- 可自动换行的RadioGroupEx,支持RadioButton设置margin
- android自定义控件自动换行效果实现
- android自定义控件自动换行效果实现
- android自定义控件实现自动换行效果
- android自定义控件自动换行效果实现
- android RadioButton实现选项卡的效果
- RadioGroup中的RadioButton实现换行
- RadioButton选中效果实现
- JXL自动换行的实现
- QLabel自动换行的实现
- css 不换行 自动换行 强制换行的实现方法
- li自动换行效果
- [CSS3] Background
- ASP.NET后台弹出Confirm对话框供用户进行操作
- Conclusion for Implementations
- css_1
- GDB的使用
- RadioButton实现自动换行的效果
- xcode工程配置及推送证书制作
- Linux下安装gcc编译器简要步骤
- window系统下的安装版tomcat安装过程
- ospf没有multipath的问题定位
- 黑马程序员——C基础之算术运算符
- 给extjs类添加默认属性
- 惭愧中的自醒,我给自己59分
- PHP面向对象 1.9 多态性的应用