RadioButton实现自动换行的效果
2015-12-07 10:40
344 查看
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); } } } }
源码
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories