如何以编程方式控制Android AbsoluteLayout中子视图的大小

时间:2009-02-18 18:24:26

标签: android layout

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中定义的内容(它是必需属性)。

2 个答案:

答案 0 :(得分:4)

在你的链接上它也说

  

不推荐使用此类。

不要花时间做一些会被删除的工作。这个课程已被弃用。

答案 1 :(得分:0)

因为它是AbsoluteLayout。 AbsoluteLayout以前已弃用。