Android自訂之流式布局,android自訂布局

來源:互聯網
上載者:User

Android自訂之流式布局,android自訂布局



流式布局,好處就是父類布局可以自動的判斷子孩子是不是需要換行,什麼時候需要換行,可以做到網頁版的標籤的效果。今天就是簡單的做了自訂的流式布局。

具體效果:




原理:
其實很簡單,Measure  Layout。只需要這兩個步驟就可以搞定了。完全的手動去Measure  Layout。
我們看一下代碼。
解釋就在代碼裡面做注釋了,因為使用為知筆記寫的部落格,格式不符合代碼格式。大家可以看具體的源碼。最後又源碼。
1.Measure  測量 
 
         @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        
        int lineHeight = 0 ;
        int lineWidth = 0 ; 
        
        int width = 0 ; 
        int height = 0 ; 
        
        int childCount = getChildCount();
        
        Log.i("Test", getPaddingLeft() + "==right="  +getPaddingRight());
        
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams params = (MarginLayoutParams)                                       childView.getLayoutParams();
            
            int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin ; 
            
            int childHeight  = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin ; 
            
            
            if ((lineWidth + childWidth ) > widthSize - getPaddingLeft() - getPaddingRight() ) {
                width = Math.max(width, lineWidth);
                lineWidth = childWidth ; 
                height += lineHeight ; 
                lineHeight = childHeight; 
            }else {
                lineWidth += childWidth ; 
                lineHeight = Math.max(lineHeight, childHeight);
            }
            
            
            if (i  == childCount-1) {
                width = Math.max(width, lineWidth);
                height += lineHeight ; 
            }
        }
        
        height += getPaddingTop() + getPaddingBottom() ;
        
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY?widthSize:width, 
                heightMode == MeasureSpec.EXACTLY?heightSize:height);
    }




2.onLayout  布局
 @Override
    protected void onLayout(boolean a, int l, int t, int r, int b) {
        
        childViewList.clear(); 
        
        int childCount = getChildCount() ; 
        int width = getWidth();
        int lineWidth = 0 ;
        int lineHeight = 0 ; 
        
        List<View> lineViews = new ArrayList<View>();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
            
            int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin ; 
            int childHeight = childView.getMeasuredHeight() + params.topMargin +  params.bottomMargin  ;
            
            if (lineWidth + childWidth > width - getPaddingLeft() - getPaddingRight()) {
                
                childViewList.add(lineViews);
                lineViews = new ArrayList<View>();
                
                if (i == 0 ) {
                    lineHeight += getPaddingTop() ; 
                }else if (i== childCount - 1) {
                    lineHeight += getPaddingBottom() ; 
                }
                this.lineHeight.add(lineHeight);
                
                lineHeight = 0 ; 
                lineWidth = 0 ; 
            }
            
            lineWidth += childWidth; 
            lineHeight = Math.max(lineHeight, childHeight) ;
            
            lineViews.add(childView);
        }
        
        childViewList.add(lineViews);
        this.lineHeight.add(lineHeight);
        
        int left = getPaddingLeft() ;
        int top = getPaddingTop(); 
        
        for (int i = 0; i < childViewList.size(); i++) {
            lineViews = childViewList.get(i);
            for (int j = 0; j < lineViews.size(); j++) {
                View childView = lineViews.get(j);
                
                MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
                
                int lc = left + params.leftMargin ; 
                int tc = top + params.topMargin ; 
                int rc = lc + childView.getMeasuredWidth()  ; 
                int bc = tc + childView.getMeasuredHeight() ; 
                
                childView.layout(lc,tc,rc,bc);
                
                left += params.leftMargin + childView.getMeasuredWidth() + params.rightMargin ; 
            }
            
            left =  getPaddingLeft() ;
            top += this.lineHeight.get(i) ; 
        }
    }


代碼:
 百度網盤:  http://pan.baidu.com/s/1hqH1kFU

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.