https://github.com/ApmeM/android-flowlayout 使用这个类库。



import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * * @author RAW */public class FlowLayout extends ViewGroup {    private final static int PAD_H = 2, PAD_V = 2; // Space between child views.    private int mHeight;    public FlowLayout(Context context) {        super(context);    }    public FlowLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);        final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();        final int count = getChildCount();        int xpos = getPaddingLeft();        int ypos = getPaddingTop();        int childHeightMeasureSpec;        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);        else            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);        mHeight = 0;        for(int i = 0; i < count; i++) {            final View child = getChildAt(i);            if(child.getVisibility() != GONE) {                child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);                final int childw = child.getMeasuredWidth();                mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);                if(xpos + childw > width) {                    xpos = getPaddingLeft();                    ypos += mHeight;                }                xpos += childw + PAD_H;            }        }        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {            height = ypos + mHeight;        } else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {            if(ypos + mHeight < height) {                height = ypos + mHeight;            }        }        height += 5; // Fudge to avoid clipping bottom of last row.        setMeasuredDimension(width, height);    } // end onMeasure()    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        final int width = r - l;        int xpos = getPaddingLeft();        int ypos = getPaddingTop();        for(int i = 0; i < getChildCount(); i++) {            final View child = getChildAt(i);            if(child.getVisibility() != GONE) {                final int childw = child.getMeasuredWidth();                final int childh = child.getMeasuredHeight();                if(xpos + childw > width) {                    xpos = getPaddingLeft();                    ypos += mHeight;                }                child.layout(xpos, ypos, xpos + childw, ypos + childh);                xpos += childw + PAD_H;            }        }    } // end onLayout()}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. RocketMQ 源码分析 —— Message 拉取与
  2. 面试官再问我如何保证 RocketMQ 不丢失消
  3. 利用神器BTrace 追踪线上 Spring Boot应
  4. 开局崩盘!IDEA 2020 无法启动的解决办法|
  5. RocketMQ 源码分析 —— 集成 Spring Boo
  6. 如何自制一个Spring Boot Starter并推送
  7. Spring Boot项目利用MyBatis Generator进
  8. 自制一个 elasticsearch-spring-boot-sta
  9. RocketMQ 源码分析 —— Message 拉取与
  10. 疫情防控不能松懈,利用数据挖掘做好重点区