我有问题。
我希望将行动态添加到TableLayout
. 添加这些行时,我需要能够获取视图的大小,因此我尝试在此期间执行此操作,onSizeChanged()
但新行不显示。所以我尝试onFinishInflate()
了,但后来我无法访问大小(getMeasuredHeight returns 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");
}
}