0

我有一个表格布局,其中我在 XML 文件中静态声明了一个按钮。我在程序中动态添加行。我希望将按钮设置在表格布局的最后。我尝试使用 android:layout_gravity="bottom" 这样做,但这没有帮助。

有什么解决办法吗?

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="horizontal|vertical"
android:scrollbarStyle="outsideInset" >
<TableLayout 
    android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <Button 
        android:id="@+id/back"
        android:layout_gravity="bottom"
    />
</TableLayout>
</ScrollView>
4

3 回答 3

0

我不相信有一种方法可以重新排序 a 的孩子ViewGroup而不删除所有孩子并以适当的顺序阅读它们。如果 aListView代替 a 就足够了TableLayout,它有一个addFooterView()方法可以实现你所追求的。

于 2012-04-21T03:25:03.253 回答
0

我不确切知道它是否适合您,但您可以做的是使用 remove 删除您的按钮,然后在添加所有元素后在运行时添加它。

例子:

    TableLayout tv = (TableLayout ) findViewbyId(R.id.table_layout_id);
    Button button = (Button) findviewbytid(R.id.button_id);

可能是您需要按钮的布局参数,以便您轻松获得它。

    LayoutParams lp = button.getLayoutParams();

从布局中删除您的按钮。

    tv.removeView(button);

在运行时添加所有视图。

再次添加您的按钮。

     tv.addView(button, lp);

我没有尝试过这段代码,但我认为它应该可以工作。在某些情况下,您不需要布局参数,因此您也可以忽略它。

于 2012-04-21T04:02:22.460 回答
0

将按钮放在表格和滚动视图之外......我的意思是在滚动视图下方

于 2012-05-02T16:46:33.160 回答