流逝布局

 流式布局的框架网址

https://blog.csdn.net/m0_37314675/article/details/78791191

 

 

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;
        }
    }
}

}

 

compile 'com.liangfeizc:flowlayout:1.0.0@aar'
 
<com.liangfeizc.flowlayout.FlowLayout android:id="@+id/flow_layout" flowlayout:vertical_spacing="10dp" flowlayout:horizontal_spacing="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" />

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值