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

如何以编程方式控制Android AbsoluteLayout中子视图的大小,android,layout,Android,Layout,网站上的文档说明: 所以我就这样推翻了它: @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; childIn

网站上的文档说明:

所以我就这样推翻了它:

@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);
}
@覆盖
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
Log.d(“测试”,“在主布局中.仅布局”);
int childCount=getChildCount();
对于(int childIndex=0;childIndex
我在XML中为布局声明子元素(按钮)


这可以正确设置按钮的位置,但不能设置大小。大小取自XML中定义的内容(这是一个必需属性)。

在您的链接上还显示

此类已弃用


不要花时间去做一些会被丢弃的东西。这个类是不推荐使用的。

因为它是绝对布局。而AbsoluteLayout以前是不推荐的

@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);
}