http://code.google.com/android/reference/android/widget/AbsoluteLayout.html上的文档说:
onLayout(boolean changed, int l, int t, int r, int b)
//Called from layout when this view should assign a size and position to each of its children.
所以我像这样覆盖它:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.d("test", "In MainLayout.onLayout");
int childCount = getChildCount();
for (int childIndex = 0; childIndex < childCount; childIndex++) {
getChildAt(childIndex).setLayoutParams(new LayoutParams(100, 100, 100, 100));
}
super.onLayout(changed, l, t, r, b);
}
我在 XML 中为布局声明子元素(按钮)。
这正确设置了按钮的位置,但没有设置大小。大小取自 XML 中定义的内容(这是必需的属性)。