ViewGroup.java
// 抽象方法,必须重写。
@Override
protected abstract void onLayout(boolean changed,
int l, int t, int r, int b);
/**
* 重写步骤
*/
protected void onLayout(boolean changed,
int l, int t, int r, int b){
// 1、遍历子view
for(int i = 0;i<getChildCount();i++){
View child = getChildAt(i);
// 2、计算子view的位置
int left,top,right,bottom;
...
// 3、确定子view的位置
child.layout(left, top, right, bottom);
}
}
获取view的宽高
View.java
// 获取view测量后宽高
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
public final int getMeasuredHeight() {
return mMeasuredHeight & MEASURED_SIZE_MASK;
}
// 获取view最终宽高
public final int getWidth() {
return mRight - mLeft;
}
public final int getHeight() {
return mBottom - mTop;
}