android设置TableLayout的高度。

| 我有一个只有XML的单行(即标题行)的TableLayout。我动态添加的所有其他行。 XML PART:
    <TableLayout android:id=\"@+id/list_table\" 
    android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" 
    android:stretchColumns=\"1\" android:background=\"#808000\" **android:scrollbars=\"vertical\">**
    <TableRow android:layout_margin=\"1dp\" android:layout_height=\"wrap_content\" 
    android:layout_width=\"wrap_content\" android:id=\"@+id/list_head_Row\">
        <TextView android:layout_width=\"wrap_content\"  
        android:text=\"Col1\" android:layout_height=\"wrap_content\"></TextView>
        <TextView android:gravity=\"center\" android:layout_width=\"wrap_content\" android:text=\"Col2\" android:layout_height=\"wrap_content\"></TextView>
        <TextView android:layout_width=\"wrap_content\" android:text=\"Col3\"  android:layout_height=\"wrap_content\"></TextView>
    </TableRow>
</TableLayout>
在Activity类中,我添加TableRows:
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
// Add 3 TextViews
// Add Row to table
t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,             LayoutParams.WRAP_CONTENT));
这样,所有行都将添加到表中并填满屏幕。我要设置的是,仅查看4-5行,其他可以滚动。为此我添加了以下内容:
    int tableHgt = t1.getChildAt(0).getHeight() + (rowHgt *40);
    t1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, tableHgt));
这样,除了xml中添加的标题行,我什么都看不到。我怎样才能使其高度仅看到5行,然后其他所有行都可以滚动?在
TableLayout
中还添加了
android:scrollbars=\"vertical\"
。 我要去哪里错了? 非常感谢您的帮助。 谢谢     
已邀请:
为什么不使用ListView? 好的...回到您的问题 将TableLayout放在ScrollView中,并将设置高度放在ScrollView中 编辑:代码 activity.java
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrolltest);

    TableLayout t1 = (TableLayout) findViewById(R.id.list_table);

    for (int i = 0; i < 10; i++) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));           
        t1.addView(tr, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));  
        TextView tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText(Integer.toString(i));
        tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText(\"Row number\");
        tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText(Integer.toString(i));        
    }
}
scrolltest.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ScrollView xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"fill_parent\" android:layout_height=\"100dp\"
    android:id=\"@+id/list_scroll\">
    <TableLayout android:id=\"@+id/list_table\"
        android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
        android:stretchColumns=\"1\">
        <TableRow android:layout_margin=\"1dp\" android:layout_height=\"wrap_content\"
            android:layout_width=\"fill_parent\" android:id=\"@+id/list_head_Row\"
            android:background=\"#808000\">
            <TextView android:layout_width=\"wrap_content\" android:text=\"Col1\"
                android:layout_height=\"wrap_content\" />
            <TextView android:gravity=\"center\" android:layout_width=\"wrap_content\"
                android:text=\"Col2\" android:layout_height=\"wrap_content\" />
            <TextView android:layout_width=\"wrap_content\" android:text=\"Col3\"
                android:layout_height=\"wrap_content\" />
        </TableRow>
    </TableLayout>
</ScrollView>
    

要回复问题请先登录注册