多列radiogroup
来源:互联网 发布:如何更改淘宝店铺名字 编辑:程序博客网 时间:2024/05/18 22:42
android SDK RadioGroup 继承LinearLayout 大家都知道 这个布局的特点,子布局线性排列,要么横要么竖, 现在呢,需要一个 类似gridview的 radiogroup,于是乎,就需要自定义RadioGroup ,重新测量、布局,进而达到下图效果
父布局是自定义的radiogroup 子布局是 sdk 默认的radiobutton
自定义的 radiogroup 下面代码就是
直接上代码:
public class GridRadioGroup extends RadioGroup {
//子布局RadioButton 列数
private int column = 3;
//文字
private int textPaddingTB = 30;
private int margin = 30;
private int itemWidth;
public GridRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridRadioGroup(Context context) {
this(context, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
itemWidth = (widthSize - (column + 1) * margin) / column;
int childCount = getChildCount();
int itemHeight = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
child.setPadding(0, textPaddingTB, 0, textPaddingTB);
child.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY),
MeasureSpec.UNSPECIFIED);
itemHeight = child.getMeasuredHeight();
}
int rows = childCount % column == 0 ? childCount / column : childCount / column + 1;
int heightSize = rows * itemHeight + (rows + 1) * margin;
setMeasuredDimension(widthMeasureSpec, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int rows = 0;
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
int yu = i % column;
int cl, ct, cr, cb;
if(i >= column - 1 && yu == 0){
rows ++;
}
cl = margin * (yu + 1) + yu * width;
ct = margin * (rows + 1) + rows * height;
cr = margin * (yu + 1) + (yu + 1) * width;
cb = margin * (rows + 1) + (rows + 1) * height;
view.layout(cl, ct, cr, cb);
}
}
}
关注微信 dream_we90
- 多列radiogroup
- 实现多行RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup
- RadioGroup的多行布局
- Android RadioGroup单选框变成多选问题
- RadioGroup出现多个RadioButton选中
- RadioGroup实现单选框的多行排列
- 自定义支持多行显示的RadioGroup
- android支持多行的radiogroup
- RadioGroup的多行显示RadioButton
- 自定义多行RadioGroup控件实现记录
- 自定义RadioGroup实现多行排列
- system调用导致子进程socket句柄泄漏问题分析
- Linux文件系统新增1--用户管理
- CISCO switches change the VTP modeautomatically from client to transparent
- webview.addJavascriptInterface() doen not work on API 16+
- Scala基础教程(二):数据类型、变量
- 多列radiogroup
- 手把手教你轻松实现listview下拉刷新
- iOS9下修改回HTTP模式进行网络请求
- 【C++设计技巧】C++中的RAII机制
- Scala基础教程(三):访问修饰符、运算符
- 一起来说 Vim 语
- Unity3D的射线检测Physics.Raycast
- 成员指针运算符 .* 和 ->*
- ZooKeeper 搭建