转载自(http://blog.csdn.net/lmj623565791/article/details/38352503)
onMeasure中计算所有childView的宽和高,然后根据childView的宽和高,计算自己的宽和高。(当然,如果不是wrap_content,直接使用父ViewGroup传入的计算值即可)
onLayout中对所有的childView进行布局。
package lud.com.myviewgroup;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by win7 on 2016/9/2.
*/
public class MyViewGroup extends ViewGroup {
//用来保存所有的view
private List<List<View>> mViews = new ArrayList<>();
//用来保存每行的高度
private List<Integer> mLineHeight = new ArrayList<>();
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(
ViewGroup.LayoutParams p) {
return new MarginLayoutParams(p);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
Log.e("generateLayoutParams", "generateLayoutParams");
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.e("onMeasure", "onMeasure");
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//记录warp_content情况下的宽高
int width = 0;
int height = 0;
int lineWidth = 0;
int lineHeight = 0;
int cCount = getChildCount();
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
int childHeight = child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
if (lineWidth + childWidth > sizeWidth) {
width = Math.max(lineWidth, childWidth);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
} else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
if (i == cCount - 1) {
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
//存储宽高
setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
: width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
: height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.e("onLayout", "onLayout");
mLineHeight.clear();
mViews.clear();
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
List<View> lineViews = new ArrayList<View>();//存放每行的view
int cCount = getChildCount();
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
if (childWidth + layoutParams.leftMargin + layoutParams.rightMargin + lineWidth > width) {
mViews.add(lineViews);
mLineHeight.add(lineHeight);
lineWidth = 0;
lineViews = new ArrayList<View>();
}
lineWidth += childWidth + layoutParams.leftMargin + layoutParams.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + layoutParams.topMargin
+ layoutParams.bottomMargin);
lineViews.add(child);
}
mLineHeight.add(lineHeight);
mViews.add(lineViews);
int left = 0;
int top = 0;
int linesNum = mViews.size();
for (int i = 0; i < linesNum; i++) {
lineViews = mViews.get(i);
lineHeight = mLineHeight.get(i);
for (int j = 0; j < lineViews.size(); j++) {
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE) {
continue;
}
MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
int lc = left + layoutParams.leftMargin;
int tc = top + layoutParams.topMargin;
int rc = lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + layoutParams.rightMargin
+ layoutParams.leftMargin;
}
left = 0;
top += lineHeight;
}
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E1E6F6"
android:orientation="vertical">
<lud.com.myviewgroup.MyViewGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="@style/text_style"
android:text="Welcome" />
<TextView
style="@style/text_style"
android:text="Welcome" />
<TextView
style="@style/text_style"
android:text="呵呵呵" />
<TextView
style="@style/text_style"
android:text="哈哈哈哈哈哈" />
<TextView
style="@style/text_style"
android:text="诶诶诶诶诶诶诶" />
<TextView
style="@style/text_style"
android:text="Welcome" />
<TextView
style="@style/text_style"
android:text="Welcome" />
<TextView
style="@style/text_style"
android:text="Welcome" />
<TextView
style="@style/text_style"
android:text="哀伤的合法地方" />
</lud.com.myviewgroup.MyViewGroup>
</LinearLayout>
效果图