admin 管理员组文章数量: 1086864
流布局
package com.bawei.myshopcar.view;import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;/*** 所有记录的流式布局,这个很熟悉*/
public class CustomFlowLayout extends LinearLayout {private int mChildMaxHeight;private int mHSpace = 20;private int mVSpace = 20;public CustomFlowLayout(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);measureChildren(widthMeasureSpec, heightMeasureSpec);findMaxChildMaxHeight();int left = 0, top = 0;int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View view = getChildAt(i);if (left != 0) {if ((left + view.getMeasuredWidth()) > sizeWidth) {top += mChildMaxHeight + mVSpace;left = 0;}}left += view.getMeasuredWidth() + mHSpace;}setMeasuredDimension(sizeWidth, (top + mChildMaxHeight) > sizeHeight ? sizeHeight : top + mChildMaxHeight);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);findMaxChildMaxHeight();int left = 0, top = 0;int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View view = getChildAt(i);if (left != 0) {if ((left + view.getMeasuredWidth()) > getWidth()) {top += mChildMaxHeight + mVSpace;left = 0;}}view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);left += view.getMeasuredWidth() + mHSpace;}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}private void findMaxChildMaxHeight() {mChildMaxHeight = 0;int childCount = getChildCount();for (int i = 0; i < childCount; i++) {View view = getChildAt(i);if (view.getMeasuredHeight() > mChildMaxHeight) {mChildMaxHeight = view.getMeasuredHeight();}}}
}
本文标签: 流布局
版权声明:本文标题:流布局 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1688013881a168813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论