自定义ViewGroup实现自动换行的布局

viewgroup简单说就是可以装view的view.今天遇到一个问题,就是需要一个可以自动根据一行中view的宽度自动换行的布局,网上找了下,没有相关的例子,但是找到了思路:自定义一个viewgroup,然后在onlayout文件里面自动检测view的右边缘的横坐标值,和你的view的parent view的况度判断是否换行显示view就可以了。因为代码比较简单,就不多说了: 

Java代码   收藏代码
  1. public class MyViewGroup extends ViewGroup {  
  2.      private final static String TAG = "MyViewGroup";  
  3.        
  4.      private final static int VIEW_MARGIN=2;  
  5.    
  6.      public MyViewGroup(Context context) {  
  7.          super(context);  
  8.      }  
  9.      @Override  
  10.      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  11.          Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);  
  12.            
  13.          for (int index = 0; index < getChildCount(); index++) {  
  14.              final View child = getChildAt(index);  
  15.              // measure  
  16.              child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);  
  17.          }  
  18.    
  19.          super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  20.      }  
  21.    
  22.      @Override  
  23.      protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {  
  24.          Log.d(TAG, "changed = "+arg0+" left = "+arg1+" top = "+arg2+" right = "+arg3+" botom = "+arg4);  
  25.          final int count = getChildCount();  
  26.          int row=0;// which row lay you view relative to parent  
  27.          int lengthX=arg1;    // right position of child relative to parent  
  28.          int lengthY=arg2;    // bottom position of child relative to parent  
  29.          for(int i=0;i<count;i++){  
  30.                
  31.              final View child = this.getChildAt(i);  
  32.              int width = child.getMeasuredWidth();  
  33.              int height = child.getMeasuredHeight();  
  34.              lengthX+=width+VIEW_MARGIN;  
  35.              lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;  
  36.              //if it can't drawing on a same line , skip to next line  
  37.              if(lengthX>arg3){  
  38.                  lengthX=width+VIEW_MARGIN+arg1;  
  39.                  row++;  
  40.                  lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;  
  41.                    
  42.              }  
  43.                
  44.              child.layout(lengthX-width, lengthY-height, lengthX, lengthY);  
  45.          }  
  46.    
  47.      }  
  48.    
  49.  }  


  这里有个地方要注意,那就要明白ViewGroup的绘图流程:ViewGroup绘制包括两个步骤:1.measure 2.layout 
  在两个步骤中分别调用回调函数:1.onMeasure()   2.onLayout() 
  1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。 
  2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。 
  3.View.MeasureSpec 与 LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署 childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。 
  4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。 
   
 

http://www.cnblogs.com/slider/archive/2011/11/24/2262161.html 

还可以试试这个: 

Java代码   收藏代码
  1. import android.content.Context;  
  2.   
  3. import android.util.AttributeSet;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.   
  7. /** 
  8.  * 
  9.  * @author RAW 
  10.  */  
  11. public class FlowLayout extends ViewGroup {  
  12.     private final static int PAD_H = 2, PAD_V = 2// Space between child views.  
  13.     private int mHeight;  
  14.   
  15.     public FlowLayout(Context context) {  
  16.         super(context);  
  17.     }  
  18.   
  19.     public FlowLayout(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.     }  
  22.   
  23.     @Override  
  24.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  25.         assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED);  
  26.         final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();  
  27.         int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();  
  28.         final int count = getChildCount();  
  29.         int xpos = getPaddingLeft();  
  30.         int ypos = getPaddingTop();  
  31.         int childHeightMeasureSpec;  
  32.         if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST)  
  33.             childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);  
  34.         else  
  35.             childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);  
  36.         mHeight = 0;  
  37.         for(int i = 0; i < count; i++) {  
  38.             final View child = getChildAt(i);  
  39.             if(child.getVisibility() != GONE) {  
  40.                 child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec);  
  41.                 final int childw = child.getMeasuredWidth();  
  42.                 mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V);  
  43.                 if(xpos + childw > width) {  
  44.                     xpos = getPaddingLeft();  
  45.                     ypos += mHeight;  
  46.                 }  
  47.                 xpos += childw + PAD_H;  
  48.             }  
  49.         }  
  50.         if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) {  
  51.             height = ypos + mHeight;  
  52.         } else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {  
  53.             if(ypos + mHeight < height) {  
  54.                 height = ypos + mHeight;  
  55.             }  
  56.         }  
  57.         height += 5// Fudge to avoid clipping bottom of last row.  
  58.         setMeasuredDimension(width, height);  
  59.     } // end onMeasure()  
  60.   
  61.     @Override  
  62.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  63.         final int width = r - l;  
  64.         int xpos = getPaddingLeft();  
  65.         int ypos = getPaddingTop();  
  66.         for(int i = 0; i < getChildCount(); i++) {  
  67.             final View child = getChildAt(i);  
  68.             if(child.getVisibility() != GONE) {  
  69.                 final int childw = child.getMeasuredWidth();  
  70.                 final int childh = child.getMeasuredHeight();  
  71.                 if(xpos + childw > width) {  
  72.                     xpos = getPaddingLeft();  
  73.                     ypos += mHeight;  
  74.                 }  
  75.                 child.layout(xpos, ypos, xpos + childw, ypos + childh);  
  76.                 xpos += childw + PAD_H;  
  77.             }  
  78.         }  
  79.     } // end onLayout()  
  80. }  



How to Implement Flipboard Animation on Android 

猜你喜欢

转载自ztlblogs.iteye.com/blog/1962667