package com.example.zh.day08;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context) {
this(context, null);
}
public MyViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inten();
}
private void inten() {
}
public int oritation = 2;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取子view的宽高
measureChildren(widthMeasureSpec, heightMeasureSpec);
int heighMeasured = 0;
int widthMeasured = 0;
//拿到view的高使用的模式模式
int heightmode = MeasureSpec.getMode(heightMeasureSpec);
//拿到view的高的大小
int heightsize = MeasureSpec.getSize(heightMeasureSpec);
//拿到view的宽的大小
int widthsize = MeasureSpec.getSize(widthMeasureSpec);
//拿到view的宽使用的模式模式
int widthmode = MeasureSpec.getMode(widthMeasureSpec);
switch (widthmode) {
//写固定的长度大小边距模式dp
case MeasureSpec.EXACTLY:
//设置view宽高
widthMeasured = widthsize;
heighMeasured = heightsize;
break;
//写wrap_content时调用的模式
case MeasureSpec.AT_MOST:
if (oritation == 1) {
int measuredHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
measuredHeight = childAt.getMeasuredHeight();
int measuredWidth = childAt.getMeasuredWidth();
widthMeasured += measuredWidth + 48;
}
heighMeasured += measuredHeight + 48;
} else if (oritation == 0) {
int measuredWidth = 0;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
int measuredHeight = childAt.getMeasuredHeight();
measuredWidth = childAt.getMeasuredWidth();
heighMeasured += measuredHeight + 48;
}
widthMeasured += measuredWidth + 48;
} else if (oritation==2) {
for (int i = 0; i <getChildCount() ; i++) {
View childAt = getChildAt(i);
int measuredHeight = childAt.getMeasuredHeight();
int measuredWidth = childAt.getMeasuredWidth();
heighMeasured+=measuredHeight+48;
widthMeasured+=measuredWidth+48;
}
}
break;
//未定义时的模式
case MeasureSpec.UNSPECIFIED:
break;
}
setMeasuredDimension(widthMeasured, heighMeasured);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//定义一个变量为子view到父view的上间距
int topall = 0;
//定义一个变量为子view到父view的左间距
int leftall = 0;
//定义一个变量为子view间的左间隔
int marginLeft = 40;
//定义一个变量为子view间的上间隔
int marginTop = 40;
//循环view里的子wiew,getChildCount子view的数量
for (int i = 0; i < getChildCount(); i++) {
//获取到子view
View childAt = getChildAt(i);
//子view的宽
int width = childAt.getMeasuredWidth();
//子view的高
int height = childAt.getMeasuredHeight();
if (oritation == 0) {
//设置子view的位置
childAt.layout(0, topall, 0 + width, topall + height);
topall += height + marginTop;
} else if (oritation == 1) {
childAt.layout(leftall, marginTop, leftall + width, marginTop + height);
leftall += width + marginLeft;
} else if (oritation==2){
childAt.layout(leftall, topall, leftall + width, height+topall);
leftall += width + marginLeft;
topall += height + marginTop;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}