如何在TableLayout中应用行跨度?

11

如何在TableLayout中应用row_span?

我想制作类似于这张图片的东西,但我不知道该怎么做。

table image

如何正确制作类似于此的内容?


看起来可能是重复的问题 https://dev59.com/QHI95IYBdhLWcg3wtwRe - Onur A.
不是重复的。那不是我问的问题的答案。 - miguel
你有没有得到这个问题的解决方案? - user4302606
2个回答

25

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 并添加任意数量的 TableRow),第二列(占用所有行空间的元素)。

enter image description here


我错过了什么吗?如何使两行等于两列?您应该在一行中放置两个表格布局以实现两列,然后在嵌套的TableLayout中放置行和列,在第二个TableLayout中放置一行或任何其他内容。然后在第二行设置span=2。 - ceph3us
对不起,这是个错误。我原来是想指两列而不是两行。问题在于,第一列包含一个TableLayout,行数可以根据需要自由调整,而第二列只包含一个视图。我会进行修改。 - Juan José Melero Gómez
使用这个作为行列表项正是我所需要的哈。 - MrJoshFisher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接