效果图
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * 自定义ViewGroup
- *
- *
- */
- public class ImageDisplayViewGroup extends ViewGroup {
- private static final int VIEW_MARGIN = 6;
- /**
- * 每个view上下的间距
- */
- private int dividerLine = 2;
- /**
- * 每个view左右的间距
- */
- private int dividerCol = 2;
- // public ImageDisplayViewGroup(Context context, AttributeSet attrs,
- // int defStyle) {
- // super(context, attrs, defStyle);
- //
- // }
- // public ImageDisplayViewGroup(Context context, AttributeSet attrs) {
- // super(context, attrs);
- // }
- public ImageDisplayViewGroup(Context context) {
- super(context);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- final int count = getChildCount();
- int row = 0;
- int lengthX = l;
- int lengthY = t;
- for (int i = 0; i < count; i++) {
- final View child = this.getChildAt(i);
- int width = child.getMeasuredWidth();
- int height = child.getMeasuredHeight();
- lengthX += width + VIEW_MARGIN;
- lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + t;
- if (lengthX > r) {
- lengthX = width + VIEW_MARGIN + l;
- row++;
- lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height
- + t;
- }
- child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
- }
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- for (int i = 0; i < getChildCount(); i++) {
- final View child = getChildAt(i);
- child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
- }
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- }
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#2F2F2F"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#2F2F2F"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/title_bg" >
- <ImageView
- android:id="@+id/iv_title_arrow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="15dip"
- android:background="@drawable/title_arrow" />
- <TextView
- android:id="@+id/tv_title_titlename"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="标题栏标题"
- android:textColor="#ffffff"
- android:textSize="18sp" />
- </RelativeLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#2F2F2F"
- android:orientation="vertical" >
- <com.pubukeji.eschoolcomm.ui.ImageDisplayViewGroup
- android:id="@+id/iv_groupview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school1" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school2" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school3" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school4" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school1" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school2" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school3" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school4" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school1" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/school2" />
- </com.pubukeji.eschoolcomm.ui.ImageDisplayViewGroup>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>