您的位置:首页 > 其它

自定义viewgroup 包含多个子view 自动换行

2016-12-05 14:57 337 查看
好记性不如烂笔头需要实现的效果如下从同事亚豪哪里的得到个自定义view ,挺好用,这里记录下来源码
package com.nf.health.app.customview;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/*** 原创作者:* yyh*/public class MyFlowLayout extends ViewGroup{private int  verticalSpacing = 10;public MyFlowLayout(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);int paddingLeft = getPaddingLeft();int paddingRight = getPaddingRight();int paddingTop = getPaddingTop();int paddingBottom = getPaddingBottom();int widthUsed = paddingLeft + paddingRight;int heightUsed = paddingTop + paddingBottom;int childMaxHeightOfThisLine = 0;int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View child = getChildAt(i);if (child.getVisibility() != GONE) {int childUsedWidth = 0;int childUsedHeight = 0;measureChild(child,widthMeasureSpec,heightMeasureSpec);childUsedWidth += child.getMeasuredWidth();childUsedHeight += child.getMeasuredHeight();LayoutParams childLayoutParams = child.getLayoutParams();MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childLayoutParams;childUsedWidth += marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;childUsedHeight += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;if (widthUsed + childUsedWidth < widthSpecSize) {//不需要换行widthUsed += childUsedWidth;//是否更新该行的高if (childUsedHeight > childMaxHeightOfThisLine) {childMaxHeightOfThisLine = childUsedHeight;}} else {//处理换行//增加View控件的高heightUsed += childMaxHeightOfThisLine + verticalSpacing;//重新计算已使用的宽widthUsed = paddingLeft + paddingRight + childUsedWidth;//换行后第一个控件的高即为行高childMaxHeightOfThisLine = childUsedHeight;}}}//View的行高=最后一行之前的高度+最后一行最高的控件的高度(行高)heightUsed += childMaxHeightOfThisLine;setMeasuredDimension(widthSpecSize, heightUsed);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int paddingLeft = getPaddingLeft();int paddingRight = getPaddingRight();int paddingTop = getPaddingTop();int paddingBottom = getPaddingBottom();int childStartLayoutX = paddingLeft;int childStartLayoutY = paddingTop;int widthUsed = paddingLeft + paddingRight;int childMaxHeight = 0;int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View child = getChildAt(i);if (child.getVisibility() != GONE) {int childNeededWidth, childNeedHeight;int left, top, right, bottom;int childMeasuredWidth = child.getMeasuredWidth();int childMeasuredHeight = child.getMeasuredHeight();LayoutParams childLayoutParams = child.getLayoutParams();MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childLayoutParams;int childLeftMargin = marginLayoutParams.leftMargin;int childTopMargin = marginLayoutParams.topMargin;int childRightMargin = marginLayoutParams.rightMargin;int childBottomMargin = marginLayoutParams.bottomMargin;childNeededWidth = childLeftMargin + childRightMargin + childMeasuredWidth;childNeedHeight = childTopMargin + childBottomMargin + childMeasuredHeight;if (widthUsed + childNeededWidth <= r - l) {if (childNeedHeight > childMaxHeight) {childMaxHeight = childNeedHeight;}left = childStartLayoutX + childLeftMargin;top = childStartLayoutY + childTopMargin;right = left + childMeasuredWidth;bottom = top + childMeasuredHeight;widthUsed += childNeededWidth;childStartLayoutX += childNeededWidth;} else {childStartLayoutY += childMaxHeight + verticalSpacing;childStartLayoutX = paddingLeft;widthUsed = paddingLeft + paddingRight;left = childStartLayoutX + childLeftMargin;top = childStartLayoutY + childTopMargin;right = left + childMeasuredWidth;bottom = top + childMeasuredHeight;widthUsed += childNeededWidth;childStartLayoutX += childNeededWidth;childMaxHeight = childNeedHeight;}child.layout(left, top, right, bottom);}}}}
使用方法,需要设置子view的param 然后调用add方法即可
	  TextView tv = (TextView) View.inflate(this, R.layout.item_selected_disease, null);ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(+ViewGroup.LayoutParams.WRAP_CONTENT, +ViewGroup.LayoutParams.WRAP_CONTENT);tv.setText(values[i]);marginLayoutParams.setMargins(10, 10, 10, 10);tv.setGravity(Gravity.CENTER);flSelectedDisease.addView(tv, marginLayoutParams);//直接调用add方法,传入子view 和子view布局参数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: