android 设置TableLayout的高度

发布于 2024-10-31 22:00:05 字数 1725 浏览 0 评论 0原文

我的 TableLayout 在 XML 中只有一行(即标题行)。其他所有行都是我动态添加的。 XML 部分:

    <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"

我哪里出错了?

非常感谢任何帮助。

谢谢

I have a TableLayout with just a single row (i.e. the Header Row) in XML. Other all rows I add dynamically.
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>

In Activity class, I add 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));

With this much all rows gets added to the table and fills the screen. I want to set is, view only 4-5 rows and others can be scrolled.For that I added :

    int tableHgt = t1.getChildAt(0).getHeight() + (rowHgt *40);
    t1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, tableHgt));

With this I can't see any row at all except the header row added in xml. How can I make its height to see just 5 rows and othe all can be scrolled ? In TableLayout android:scrollbars="vertical" is also added.

Where am I going wrong ?

Any help is highly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-11-07 22:00:05

你为什么不使用 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>

why don't you use ListView ?

but ok ... back to your question
put TableLayout in ScrollView and setup height in ScrollView

EDIT: code

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文