TableLayout出现在另一个TableLayout之上

时间:2012-12-20 03:09:09

标签: android android-layout

我有以下定义:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/leftContainer"
    android:layout_weight="0.8">
    <TableLayout
        android:id="@+id/drinksTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:padding="0dp" />
    <TableLayout
        android:id="@+id/itemsTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:padding="0dp"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

两种表格布局都是以编程方式构建的。

我希望第二个表格直接显示在第一个表格下方,但我能做的最好就是让它坐在我不喜欢的布局底部(使用alignParentBottom)。

如何让itemsTable直接显示在drinksTable下方?

1 个答案:

答案 0 :(得分:1)

只需将您的布局转换为LinearLayout,确保添加android:orientation="vertical";这将使每个元素垂直堆叠。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/leftContainer"
    android:layout_weight="0.8"
    android:orientation="vertical">
    <TableLayout
        android:id="@+id/drinksTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:padding="0dp" />
    <TableLayout
        android:id="@+id/itemsTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:padding="0dp"
        android:layout_alignParentBottom="true" />
</LinearLayout>

但请注意,如果元素数量过多,它们将从底部的屏幕流出。 (您可能希望将此LinearLayout包装在ScrollView中。)