10

How to apply row_span to TableLayout?

I want to make something similar to this image, but i don't know how to do it.

table image

What is the correct way to make something like this?

4

2 回答 2

24

TableLayout不支持行跨度,仅支持列跨度。GridLayout支持行和列跨度:

网格布局示例

(图片来自http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html

于 2013-04-20T23:38:33.103 回答
8

小作弊(代码可能相当复杂,仅对简单设计有效):

做一个TableLayout. 将另一个TableLayout放在第一列和一个您希望在第二列中具有行跨度的视图。这个内部TableLayout可以有尽可能多TableRows的你希望另一个视图跨越。

这是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin"
    tools:context=".MainActivity">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3">

            <TableLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_dark">

                    <TextView
                        android:text="Row 1"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_light">

                    <TextView
                        android:text="Row 2"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_blue_bright">

                    <TextView
                        android:text="Row 3"/>

                </TableRow>

            </TableLayout>

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.5"
                android:text="Second column, rowspan 3"
                android:background="@android:color/holo_purple"
                android:gravity="center"/>

        </TableRow>

    </TableLayout>

</LinearLayout>

所以,你总结一下:

TableLayout:第一列(TableLayout 与我们想​​要的尽可能多的 TableRows),第二列(将占用所有这些行空间的元素)。

在此处输入图像描述

于 2015-04-25T22:39:32.033 回答