无锡网站制作网站,燕郊做网站的,南浔哪有做网站的,排名前十的室内设计公司Android可换行的RadioGroup,有时候需要换行显示的单选列表#xff0c;当然可以有多种实现方式#xff0c;比如recycleview或者listview实现#xff0c;本文采用的是RadioGrouprediobutton方式实现。 一、首先自定义view public class WrapRadioGroup extends RadioGroup {pr…        Android可换行的RadioGroup,有时候需要换行显示的单选列表当然可以有多种实现方式比如recycleview或者listview实现本文采用的是RadioGrouprediobutton方式实现。 一、首先自定义view public class WrapRadioGroup extends RadioGroup {private static final String TAG  RadioGroupEx;public WrapRadioGroup(Context context) {super(context);}public WrapRadioGroup(Context context, AttributeSet attrs) {super(context, attrs);}Overrideprotected 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的方法测量子viewmeasureChildren(widthMeasureSpec, heightMeasureSpec);//最大的宽int maxWidth  0;//累计的高int totalHeight  0;//当前这一行的累计行宽int lineWidth  0;//当前这行的最大行高int maxLineHeight  0;//用于记录换行前的行宽和行高int oldHeight;int oldWidth;int count  getChildCount();//假设 widthMode和heightMode都是AT_MOSTfor (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的宽,所以用的是oldWidthmaxWidth  Math.max(lineWidth, oldWidth);//重置宽度lineWidth  deltaX;//累加高度totalHeight  oldHeight;//重置行高,当前这个View属于下一行因此当前最大行高为这个child的高度加上marginmaxLineHeight  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);}Overrideprotected 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;}}}二、布局直接引用 
LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalcsu.xiaoya.robotApp.ui.view.WrapRadioGroupandroid:idid/rg_blsandroid:layout_width438dpandroid:layout_height179dpandroid:layout_belowid/monitor_remdandroid:layout_alignParentRighttrueandroid:layout_marginTopdimen/dp_15android:layout_marginRightdimen/dp_24android:orientationhorizontalandroid:padding1dpapp:maxWidth300dpRadioButtonandroid:idid/rb_date_dayandroid:layout_widthdimen/dp_84android:layout_heightdimen/dimen_48android:backgrounddrawable/bls_am_2h_sgandroid:buttonnullandroid:checkedtrueandroid:layout_marginLeftdimen/dp_10android:gravitycenterandroid:text随机血糖android:textColorcolor/whiteandroid:textSizedimen/sp_10 /RadioButtonandroid:idid/rb_date_weekandroid:layout_widthdimen/dp_84android:layout_heightdimen/dimen_48android:layout_marginLeftdimen/dp_10android:backgrounddrawable/bls_am_2h_sgandroid:buttonnullandroid:gravitycenterandroid:text空腹血糖android:textColorcolor/whiteandroid:textSizedimen/sp_10 /RadioButtonandroid:layout_widthdimen/dp_84android:layout_heightdimen/dimen_48android:layout_marginLeftdimen/dp_10android:backgrounddrawable/bls_am_2h_sgandroid:buttonnullandroid:gravitycenterandroid:text早餐后2小时android:textColorcolor/whiteandroid:textSizedimen/sp_10 /RadioButtonandroid:layout_widthdimen/dp_84android:layout_heightdimen/dimen_48android:layout_marginLeftdimen/dp_10android:backgrounddrawable/bls_am_2h_sgandroid:buttonnullandroid:gravitycenterandroid:text早餐后2小时android:textColorcolor/whiteandroid:textSizedimen/sp_10 /RadioButtonandroid:layout_widthdimen/dp_84android:layout_heightdimen/dimen_48android:layout_marginLeftdimen/dp_10android:layout_marginTopdimen/dp_10android:backgrounddrawable/bls_am_2h_sgandroid:buttonnullandroid:gravitycenterandroid:text早餐后2小时android:textColorcolor/whiteandroid:textSizedimen/sp_10 //csu.xiaoya.robotApp.ui.view.WrapRadioGroup/LinearLayout 
三、背景样式bls_am_2h_sg 
?xml version1.0 encodingutf-8?
selector xmlns:androidhttp://schemas.android.com/apk/res/androiditem android:width84dp android:height48dp android:state_checkedfalseshape android:shaperectanglesolid android:color#ff27b074 /corners android:bottomLeftRadius5dp android:bottomRightRadius5dp android:topLeftRadius5dp android:topRightRadius5dp //shape/itemitem android:width88dp android:height50dp android:state_checkedtrueshape android:shaperectanglesolid android:color#ff27b074 /corners android:bottomLeftRadius5dp android:bottomRightRadius5dp android:topLeftRadius5dp android:topRightRadius5dp //shape/item
/selector 四、大功告成