package com.taiwan.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
public class MyViewGroup extends ViewGroup {
private DisplayMetrics displayMetrics;
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
displayMetrics = context.getResources().getDisplayMetrics();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println("onMeasure="+width+"="+height);
for (int i = 0; i <getChildCount(); i++) {
View childAt = getChildAt(i);
// childAt.measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
childAt.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
//int measuredWidth = childAt.getMeasuredWidth();
//int measuredHeight = childAt.getMeasuredHeight();
//System.out.println("onMeasure/childAt="+measuredWidth+"="+measuredHeight);
}
setMeasuredDimension(width, height);
}
/* (l,t)左上角 距离父类左边 顶部的距离
* (r,b)右下角距离父类左边 顶部的距离
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println("onLayout"+l+"/"+t+"/"+r+"/"+b);
int[][] locations=new int[6][4];
//childAt的宽高(296,314)
locations[0]=new int[]{120,188,120+296,188+314};
locations[1]=new int[]{532,88,532+296,88+314};
locations[2]=new int[]{51,642,51+296,642+314};
locations[3]=new int[]{373,432,373+296,432+314};
locations[4]=new int[]{762,448,762+296,448+314};
locations[5]=new int[]{551,739,551+296,739+314};
for (int i = 0; i <getChildCount(); i++) {
View childAt = getChildAt(i);
int left = locations[i][0];
int top = locations[i][1];
int right = locations[i][2];
int bottom = locations[i][3];
childAt.layout(left, top, right, bottom);
}
}
}