android 流布局实例

特别好用的util,啥都不说了直接上代码,下面有现在地址哦



import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;



public class AutoUtil extends ViewGroup{

    public AutoUtil(Context context) {
        super(context);
    }

    public AutoUtil(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoUtil(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onLayout(boolean br, int l, int t, int r, int b) {

        int topPadding=getPaddingTop();
        int rawHeight=0;
        int viewWidth=r-l;
        int leftOffer=getPaddingLeft();//横向的偏移量
        View child;
        for (int i=0,count=getChildCount();i<count;i++){
            child=getChildAt(i);
            if (child.getVisibility()==GONE) continue;
            LayoutParams params=child.getLayoutParams();
            MarginLayoutParams lp=null;
            if (params instanceof MarginLayoutParams){
                lp= (MarginLayoutParams) params;
            }else {
                lp=new MarginLayoutParams(params);
            }
            int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            if (childWidth+leftOffer+getPaddingRight()>viewWidth){
                leftOffer=getPaddingLeft();
                topPadding+=rawHeight;
                rawHeight=0;
            }
            int ll=lp.leftMargin+leftOffer;
            int tl=topPadding+lp.topMargin;
            int rl=leftOffer+lp.rightMargin+child.getMeasuredWidth();
            int bl=topPadding+lp.topMargin+child.getMeasuredHeight();
            child.layout(ll,tl,rl,bl);
            leftOffer+=childWidth;
            int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;
            rawHeight=Math.max(rawHeight,childHeight);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);
        int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);

        int wedth=0;
        int height=0;
        int rawWidth=0;
        int rawHeight=0;
        int count=getChildCount();
        for (int i=0;i<count;i++){
            View view=getChildAt(i);
            if (view.getVisibility()==GONE){
                if (i==count-1){
                    wedth=Math.max(wedth,rawWidth) ;
                    height+=rawHeight;
                }
                continue;
            }
            measureChildWithMargins(view,widthMeasureSpec,0,heightMeasureSpec,0);
            LayoutParams params=view.getLayoutParams();
            MarginLayoutParams lp=null;
            if (params instanceof MarginLayoutParams){
                lp= (MarginLayoutParams) params;
            }else {
                lp=new MarginLayoutParams(params);
            }

            int viewWight=view.getWidth()+lp.leftMargin+lp.rightMargin;
            int viewHeight =view.getHeight()+lp.topMargin+lp.bottomMargin;
            if (viewWight+rawWidth>widthMeasureSpec-getPaddingLeft()-getPaddingRight()){

                wedth=Math.max(wedth,viewWight);
                rawWidth=viewWight;
                height=rawHeight;
                rawHeight=viewHeight;

            }else {
                wedth+=viewWight;
                height=Math.max(rawHeight,viewHeight);
            }
            if (i==count-1){
                wedth=Math.max(rawWidth,wedth);
                height+=rawHeight;
            }
        }
        setMeasuredDimension(widthSpecMode==MeasureSpec.EXACTLY?wedth:widthSpecSize,heightSpecMode==MeasureSpec.EXACTLY?height:heightSpecSize);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    }
}

大家赋值黏贴就可以使用喽 下载链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值