自定义流式布局控件FlowLayout
来源:互联网 发布:怎么看淘宝里的买家秀 编辑:程序博客网 时间:2024/04/19 16:33
/** * 流式布局 FlowLayout 用法和LineLayout一样,可自动换行 */public class FlowLayout extends ViewGroup { private static final int DEFAULT_HORIZONTAL_SPACING = 5; private static final int DEFAULT_VERTICAL_SPACING = 5; private int mVerticalSpacing; private int mHorizontalSpacing; public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout); try { mHorizontalSpacing = a.getDimensionPixelSize( R.styleable.FlowLayout_horizontal_spacing, DEFAULT_HORIZONTAL_SPACING); mVerticalSpacing = a.getDimensionPixelSize( R.styleable.FlowLayout_vertical_spacing, DEFAULT_VERTICAL_SPACING); } finally { a.recycle(); } } public void setHorizontalSpacing(int pixelSize) { mHorizontalSpacing = pixelSize; } public void setVerticalSpacing(int pixelSize) { mVerticalSpacing = pixelSize; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int myWidth = resolveSize(0, widthMeasureSpec); int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int paddingBottom = getPaddingBottom(); int childLeft = paddingLeft; int childTop = paddingTop; int lineHeight = 0; // Measure each child and put the child to the right of previous child // if there's enough room for it, otherwise, wrap the line and put the child to next line. for (int i = 0, childCount = getChildCount(); i < childCount; ++i) { View child = getChildAt(i); if (child.getVisibility() != View.GONE) { measureChild(child, widthMeasureSpec, heightMeasureSpec); } else { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childLeft + childWidth + paddingRight > myWidth) { childLeft = paddingLeft; childTop += mVerticalSpacing + lineHeight; lineHeight = childHeight; } else { childLeft += childWidth + mHorizontalSpacing; } } int wantedHeight = childTop + lineHeight + paddingBottom; setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int myWidth = r - l; int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int childLeft = paddingLeft; int childTop = paddingTop; int lineHeight = 0; for (int i = 0, childCount = getChildCount(); i < childCount; ++i) { View childView = getChildAt(i); if (childView.getVisibility() == View.GONE) { continue; } int childWidth = childView.getMeasuredWidth(); int childHeight = childView.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childLeft + childWidth + paddingRight > myWidth) { childLeft = paddingLeft; childTop += mVerticalSpacing + lineHeight; lineHeight = childHeight; } childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mHorizontalSpacing; } }}
需要添加的style
<declare-styleable name="FlowLayout"> <attr name="horizontal_spacing" format="dimension" /> <attr name="vertical_spacing" format="dimension" /></declare-styleable>
0 0
- 自定义流式布局控件FlowLayout
- 自定义控件之-流式布局FlowLayout
- Android自定义控件--流式布局(FlowLayout)
- Android自定义控件--流式布局(FlowLayout)--自动适配
- 自定义流式布局FlowLayout
- 自定义流式布局FlowLayout
- 自定义控件之流式布局FlowLayout
- 自定义ViewGroup实现流式布局FlowLayout
- Android自定义流式布局-FlowLayout
- 自定义ViewGroup,流式布局FlowLayout
- Android自定义流式布局-FlowLayout
- android_studio自定义FlowLayout流式布局
- FlowLayout实现自定义的流式布局
- FlowLayout流式布局
- FlowLayout流式布局
- 流式布局FlowLayout
- 流式布局FlowLayout
- 流式布局 FlowLayout
- UITabBarControllerr的使用
- 神奇的马甲Dll By 海风月影
- PHP和MySQL的timestamp转换
- 高并发程序设计入门(转)
- iOS App图标及尺寸
- 自定义流式布局控件FlowLayout
- 分布式文件存储FastDFS(七)FastDFS配置文件详解
- less 命令翻页键 列表
- Linux初讲——系统命令行常用快捷键
- secureCRT rz sz 详解
- vlfeat dense sift理解
- 新学期 新生活
- 什么是java字节码文件
- Android JSBridge的原理与实现