自定义ViewGroup实现水平布局空间不足自动换行的效果

今天带着大家一起来实现这样的一个效果,当水平方向空间不足无法显示下一个子View时自动换行,看到这样的效果首先就会想到自定义viewgroup,那么该如何实现呢,好,废话不好说。看代码:

public class MyViewGroup extends ViewGroup {


public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


public MyViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public MyViewGroup(Context context) {
this(context, null);
}


private int DEFAULT_WIDTH = 200;//默认的宽度
private int DEFAULT_HEIGHT = 200;//默认的高度


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getSize(DEFAULT_WIDTH, widthMeasureSpec);
int height = getSize(DEFAULT_HEIGHT, heightMeasureSpec);


measureChildren(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(width, height);
}


private int getSize(int defaultSize, int widthMeasureSpec) {
int result = 0;
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else if (mode == MeasureSpec.AT_MOST) {
result = Math.min(defaultSize, size);
} else {
result = defaultSize;
}
return result;
}


@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {
int mWidth = getMeasuredWidth();
int childX = l;
int childY = t;
for (int index = 0; index < getChildCount(); index++) {
View view = getChildAt(index);
int childWidth = view.getMeasuredWidth();
int childHeight = view.getMeasuredHeight();
MyViewGroup.LayoutParams param = (LayoutParams) view
.getLayoutParams();
if (childX + childWidth + param.leftMargin + param.rightMargin > mWidth) {// 如果宽度大于父控件的宽度,则换行
childY += childHeight + param.topMargin;
childX = l;
}


view.layout(childX + param.leftMargin, childY + param.topMargin,
childX + childWidth + param.leftMargin, childY
+ childHeight + param.topMargin);


childX += childWidth + param.leftMargin + param.rightMargin;
}
}


public static class LayoutParams extends ViewGroup.MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
}


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


}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <com.example.onlayoutdemo.MyViewGroup
        android:id="@+id/myViewGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#aabbcc" >


        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#aacc00" />


        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#aa00ff" />


        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#aa0000" />


        <View
            android:layout_width="300dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#aaff00" />


        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#ff0000" />


        <View
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#000000" />


        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:background="#aa00dd" />
    </com.example.onlayoutdemo.MyViewGroup>


</RelativeLayout>

ManActivity:

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


}

OK,效果实现。

猜你喜欢

转载自blog.csdn.net/xadlovezy/article/details/43850827
今日推荐