动态添加TableRow与getMeasuredHeight/getHeight存在问题

3
我遇到了一个问题。
我希望能够动态添加行到一个TableLayout中。在添加这些行时,我需要获取视图的大小,所以我尝试在onSizeChanged()期间执行此操作,但新行不会显示出来。因此,我尝试使用onFinishInflate(),但是我无法访问大小(getMeasuredHeight返回0)。
以下代码的输出显示了两行initial row + onFinishInflate getMeasuredHeight=0。但是,如果我使用sdk中的hierarchyviewer并按load view hierarchy,则在模拟器中突然看到3行initial row + onFinishInflate getMeasuredHeight=0 + onSizeChanged getMeasuredHeight=270!。
注意:这是在emulator 2.1 hvga landscape上使用的,使用最新的sdk构建的代码,目标为android1.5。
<?xml version="1.0" encoding="utf-8"?>
<com.steelbytes.android.TableHeightTest.TestLinLay 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
                <TextView
                    android:text="initial row"
                />
            </TableRow>
        </TableLayout>
</com.steelbytes.android.TableHeightTest.TestLinLay>

package com.steelbytes.android.TableHeightTest;

import android.app.Activity;
import android.os.Bundle;

public class TableHeightTest extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

package com.steelbytes.android.TableHeightTest;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestLinLay extends LinearLayout
{
 Context mContext;

 public TestLinLay(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  mContext = context;
 }

    private void addRow(String funcName)
    {
        TableLayout tl = (TableLayout)findViewById(R.id.table1);
        TableRow tr = new TableRow(mContext);
        TextView tv = new TextView(mContext);
        tv.setText(funcName+" getMeasuredHeight="+tl.getMeasuredHeight());
        tr.addView(tv);
        tl.addView(tr);
    }

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        addRow("onFinishInflate");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w,h,oldw,oldh);
        addRow("onSizeChanged");
    }
}


忘了提到,添加调用requestLayout()和/或invalidate()也没有帮助。 - SteelBytes
2个回答

1
我曾经遇到过同样的问题。最终我偶然发现了以下“规则”: 在“布局传递”(onSizeChanged())期间,您不能/不应该请求requestLayout(),正确的方法是“发布一个Runnable”稍后再执行。这是我在我的onSizeChanged()重写完成后使用的代码(我已经调整了所有按钮的大小)。
// Note: 'posting a Runnable' is required to call requestLayout(),
// not supposed to call requestLayout() during a 
// layout pass (onSizeChanged())
private Handler handler;        // for posting request(s)

(In constructor code)
{
    handler = new Handler();
}

/*
 * Post request to run a loop requesting button layout so
 * the buttons will be drawn using their new size.
 */
private void postLayoutRequest() {
handler.post(new Runnable() {
    @Override public void run() {
    for(int i = BTN_BASE_ID; i <= TOP_BUTTON_ID; ++i) {
            ButtonEx btn = (ButtonEx)findViewById(i);
            btn.requestLayout();
        }
    }
});
}

可以使用View.post代替handler。非常感谢您的解释,您救了我的一天。 - neworld

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接