前几天转载了一个项目中经常会用到的flowlayout,大神写的很详细,需要了可以去看看,今天再分享一个不一样的flowlayout,背景可自动填充屏幕宽度,展示效果较好,可以先看下:
如图不管几个标签总是占满一行,下面就看看是如何自定义的注释很详细就直接粘代码了:
public class FlowLayout extends ViewGroup{ public static final int DEFAULT_SPACING = 20; /** 横向间隔 */ private int mHorizontalSpacing = DEFAULT_SPACING; /** 纵向间隔 */ private int mVerticalSpacing = DEFAULT_SPACING; /** 是否需要布局,只用于第一次 */ boolean mNeedLayout = true; /** 当前行已用的宽度,由子View宽度加上横向间隔 */ private int mUsedWidth = 0; /** 代表每一行的集合 */ private final List<Line> mLines = new ArrayList<Line>(); private Line mLine = null; /** 最大的行数 */ private int mMaxLinesCount = Integer.MAX_VALUE; public FlowLayout(Context context) { super(context); } public void setHorizontalSpacing(int spacing) { if (mHorizontalSpacing != spacing) { mHorizontalSpacing = spacing; requestLayoutInner(); } } public void setVerticalSpacing(int spacing) { if (mVerticalSpacing != spacing) { mVerticalSpacing = spacing; requestLayoutInner(); } } public void setMaxLines(int count) { if (mMaxLinesCount != count) { mMaxLinesCount = count; requestLayoutInner(); } } private void requestLayoutInner() { UiUtils.runOnUiThread(new Runnable() { @Override public void run() { requestLayout(); } }); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int sizeWidth = View.MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight() - getPaddingLeft(); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); restoreLine();// 还原数据,以便重新记录 final int count = getChildCount(); for (int i = 0; i < count; </