public class RelativLayout extends ViewGroup {
int groupHeight, groupWidth;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
groupHeight = h;
groupWidth = w;
}
public RelativLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
// 测量每个子控件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 得到子控件个数
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
// 得到一个子控件
View childView = this.getChildAt(i);
// 测量子控件
childView.measure(widthMeasureSpec, heightMeasureSpec);
}
}
// 指定子控件如何显示
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 找到第一个子控件
View view1 = this.getChildAt(0);
View view2 = this.getChildAt(1);
// 模拟出RelativLayout 指定相对布局的位置
view1.layout(0, 0, ?, ?);
view2.layout(?, ?, ?, ?);
}
int groupHeight, groupWidth;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
groupHeight = h;
groupWidth = w;
}
public RelativLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
// 测量每个子控件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 得到子控件个数
int childCount = this.getChildCount();
for (int i = 0; i < childCount; i++) {
// 得到一个子控件
View childView = this.getChildAt(i);
// 测量子控件
childView.measure(widthMeasureSpec, heightMeasureSpec);
}
}
// 指定子控件如何显示
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 找到第一个子控件
View view1 = this.getChildAt(0);
View view2 = this.getChildAt(1);
// 模拟出RelativLayout 指定相对布局的位置
view1.layout(0, 0, ?, ?);
view2.layout(?, ?, ?, ?);
}