效果图

重写ViewGroup 实现组件自动换行_重写ViewGroup

  1.  
  2.  
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.  
  7. /**  
  8.  * 自定义ViewGroup  
  9.  *   
  10.   
  11.  *   
  12.  */ 
  13. public class ImageDisplayViewGroup extends ViewGroup {  
  14.     private static final int VIEW_MARGIN = 6;  
  15.     /**  
  16.      * 每个view上下的间距  
  17.      */ 
  18.     private int dividerLine = 2;  
  19.     /**  
  20.      * 每个view左右的间距  
  21.      */ 
  22.     private int dividerCol = 2;  
  23.  
  24.     // public ImageDisplayViewGroup(Context context, AttributeSet attrs,  
  25.     // int defStyle) {  
  26.     // super(context, attrs, defStyle);  
  27.     //  
  28.     // }  
  29.  
  30.     // public ImageDisplayViewGroup(Context context, AttributeSet attrs) {  
  31.     // super(context, attrs);  
  32.     // }  
  33.  
  34.     public ImageDisplayViewGroup(Context context) {  
  35.         super(context);  
  36.     }  
  37.  
  38.     @Override 
  39.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  40.  
  41.         final int count = getChildCount();  
  42.         int row = 0;  
  43.         int lengthX = l;  
  44.         int lengthY = t;  
  45.         for (int i = 0; i < count; i++) {  
  46.  
  47.             final View child = this.getChildAt(i);  
  48.             int width = child.getMeasuredWidth();  
  49.             int height = child.getMeasuredHeight();  
  50.             lengthX += width + VIEW_MARGIN;  
  51.             lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + t;  
  52.             if (lengthX > r) {  
  53.                 lengthX = width + VIEW_MARGIN + l;  
  54.                 row++;  
  55.                 lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height  
  56.                         + t;  
  57.  
  58.             }  
  59.  
  60.             child.layout(lengthX - width, lengthY - height, lengthX, lengthY);  
  61.         }  
  62.     }  
  63.  
  64.     @Override 
  65.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  66.         for (int i = 0; i < getChildCount(); i++) {  
  67.             final View child = getChildAt(i);  
  68.             child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);  
  69.         }  
  70.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  71.     }  
  72.  
  73. }  

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.     android:background="#2F2F2F" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <LinearLayout 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:background="#2F2F2F" 
  11.         android:orientation="vertical" > 
  12.  
  13.         <RelativeLayout 
  14.             android:layout_width="match_parent" 
  15.             android:layout_height="wrap_content" 
  16.             android:background="@drawable/title_bg" > 
  17.  
  18.             <ImageView 
  19.                 android:id="@+id/iv_title_arrow" 
  20.                 android:layout_width="wrap_content" 
  21.                 android:layout_height="wrap_content" 
  22.                 android:layout_alignParentLeft="true" 
  23.                 android:layout_centerVertical="true" 
  24.                 android:layout_marginLeft="15dip" 
  25.                 android:background="@drawable/title_arrow" /> 
  26.  
  27.             <TextView 
  28.                 android:id="@+id/tv_title_titlename" 
  29.                 android:layout_width="wrap_content" 
  30.                 android:layout_height="wrap_content" 
  31.                 android:layout_centerInParent="true" 
  32.                 android:text="标题栏标题" 
  33.                 android:textColor="#ffffff" 
  34.                 android:textSize="18sp" /> 
  35.         </RelativeLayout> 
  36.  
  37.         <LinearLayout 
  38.             android:layout_width="wrap_content" 
  39.             android:layout_height="wrap_content" 
  40.             android:background="#2F2F2F" 
  41.             android:orientation="vertical" > 
  42.  
  43.             <com.pubukeji.eschoolcomm.ui.ImageDisplayViewGroup 
  44.                 android:id="@+id/iv_groupview" 
  45.                 android:layout_width="wrap_content" 
  46.                 android:layout_height="wrap_content" > 
  47.  
  48.                 <ImageView 
  49.                     android:layout_width="wrap_content" 
  50.                     android:layout_height="wrap_content" 
  51.                     android:src="@drawable/school1" /> 
  52.  
  53.                 <ImageView 
  54.                     android:layout_width="wrap_content" 
  55.                     android:layout_height="wrap_content" 
  56.                     android:src="@drawable/school2" /> 
  57.  
  58.                 <ImageView 
  59.                     android:layout_width="wrap_content" 
  60.                     android:layout_height="wrap_content" 
  61.                     android:src="@drawable/school3" /> 
  62.  
  63.                 <ImageView 
  64.                     android:layout_width="wrap_content" 
  65.                     android:layout_height="wrap_content" 
  66.                     android:src="@drawable/school4" /> 
  67.  
  68.                 <ImageView 
  69.                     android:layout_width="wrap_content" 
  70.                     android:layout_height="wrap_content" 
  71.                     android:src="@drawable/school1" /> 
  72.  
  73.                 <ImageView 
  74.                     android:layout_width="wrap_content" 
  75.                     android:layout_height="wrap_content" 
  76.                     android:src="@drawable/school2" /> 
  77.  
  78.                 <ImageView 
  79.                     android:layout_width="wrap_content" 
  80.                     android:layout_height="wrap_content" 
  81.                     android:src="@drawable/school3" /> 
  82.  
  83.                 <ImageView 
  84.                     android:layout_width="wrap_content" 
  85.                     android:layout_height="wrap_content" 
  86.                     android:src="@drawable/school4" /> 
  87.  
  88.                 <ImageView 
  89.                     android:layout_width="wrap_content" 
  90.                     android:layout_height="wrap_content" 
  91.                     android:src="@drawable/school1" /> 
  92.  
  93.                 <ImageView 
  94.                     android:layout_width="wrap_content" 
  95.                     android:layout_height="wrap_content" 
  96.                     android:src="@drawable/school2" /> 
  97.             </com.pubukeji.eschoolcomm.ui.ImageDisplayViewGroup> 
  98.         </LinearLayout> 
  99.     </LinearLayout> 
  100.  
  101. </LinearLayout>