public class RadioGroupEx extends RadioGroup {
private static final String TAG = "RadioGroupEx";
public RadioGroupEx(Context context) {
super(context);
}
public RadioGroupEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//调用ViewGroup的方法,测量子view
measureChildren(widthMeasureSpec, heightMeasureSpec);
//最大的宽
int maxWidth = 0;
//累计的高
int totalHeight = 0;
//当前这一行的累计行宽
int lineWidth = 0;
//当前这行的最大行高
int maxLineHeight = 0;
//用于记录换行前的行宽和行高
int oldHeight;
int oldWidth;
int count = getChildCount();
//假设 widthMode和heightMode都是AT_MOST
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
//得到这一行的最高
oldHeight = maxLineHeight;
//当前最大宽度
oldWidth = maxWidth;
int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
//和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth
maxWidth = Math.max(lineWidth, oldWidth);
//重置宽度
lineWidth = deltaX;
//累加高度
totalHeight += oldHeight;
//重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin
maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);
} else {
//不换行,累加宽度
lineWidth += deltaX;
//不换行,计算行最高
int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
maxLineHeight = Math.max(maxLineHeight, deltaY);
}
if (i == count - 1) {
//前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值
totalHeight += maxLineHeight;
//计算最后一行和前面的最宽的一行比较
maxWidth = Math.max(lineWidth, oldWidth);
}
}
//加上当前容器的padding值
maxWidth += getPaddingLeft() + getPaddingRight();
totalHeight += getPaddingTop() + getPaddingBottom();
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
//pre为前面所有的child的相加后的位置
int preLeft = getPaddingLeft();
int preTop = getPaddingTop();
//记录每一行的最高值
int maxHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
//r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。
if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
//重置
preLeft = getPaddingLeft();
//要选择child的height最大的作为设置
preTop = preTop + maxHeight;
maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
} else { //不换行,计算最大高度
maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
}
//left坐标
int left = preLeft + params.leftMargin;
//top坐标
int top = preTop + params.topMargin;
int right = left + child.getMeasuredWidth();
int bottom = top + child.getMeasuredHeight();
//为子view布局
child.layout(left, top, right, bottom);
//计算布局结束后,preLeft的值
preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
}
}
}
<com.example.duohangdanxuankuang.RadioGroupEx
android:id="@+id/yanse"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/y"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="黄色" />
<RadioButton
android:id="@+id/b"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="蓝色" />
<RadioButton
android:id="@+id/r"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="红色" />
<RadioButton
android:id="@+id/g"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="灰色" />
<RadioButton
android:id="@+id/w"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="白色" />
<RadioButton
android:id="@+id/bl"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="黑色" />
</com.example.duohangdanxuankuang.RadioGroupEx>yanse=(RadioGroup)findViewById(R.id.yanse);
yanse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.y:
Toast.makeText(MainActivity.this,"黄色",Toast.LENGTH_LONG).show();
break;
}
}
});
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java swing单选框换行
1.JCheckBox(复选框) (1)常用方法: *JCheckBox(String label) *JCheckBox(String label
java swing单选框换行 ide 滑块 组合框 -
java获取单选框的值 javaswing单选框
java swing表格中包含单选框
表格中包含复杂控件 java 控件 ide -
html5 单选框 多选框 html的单选框
image.pngHTML标记语言,网页制作的第一步。什么是HTML呢?查百度基础语法常用标签HTML是超文本标记语言,HTML不用编译,直接在浏览器中执行,HTML是一个文本文件。HTML基本结构,标签,元素,属性,注解 <html> 标签属性格式: <标签名 文档类型声明: <!DOCTYPE> 声明必须放在html文档第一行,不是
html5 单选框 多选框 html 单选框 html 表格table点一下变色 html 表格中单选框触发事件 html下拉复选框 -
java 单选框 awt java单选框代码
本文实例讲述了java之swing单选框用法。分享给大家供大家参考。
java单选框赋值判断 java java程序设计 单选框 -
python实现单选框 python获取单选框的值
在PyQt5中,制作程序UI界面,一般可以通过UI制作工具和纯代码编写两种方式来实现。Qt Designer是什么Qt Designer,是一个强大、灵活的可视化GUI设计工具,可以帮助我们提高开发PyQt程序的效率。Qt Designer是专门用来制作PyQt程序中UI界面的工具,它生产的UI界面以.ui后缀结尾。这个.ui文件使用起来也很简单,咱们通过命令的方式即可以将.ui转换成.py格式的
python实现单选框 python获取单选框的值 qt designer文本输入框 qt5 多页面跳转 python Qt -
cnn 和 transformer融合
SwinT模块,让Swin-Transformer 的使用变得和CNN一样方便快捷!项目内容一、SwinT模块的使用演示,接口酷似Conv2D由于以下两点原因,我们将Swin-Transformer最核心的部分制成了一个类似于nn.Conv2D的接口并命名为SwinT。其输入、输出数据形状完全和Conv2D(CNN)一样,这极大的方便了使用Transformer来编写模型代码。1、一方面,虽然随着
cnn 和 transformer融合 transformer cnn paddlepaddle paddle