Android TableLayout中"colspan"的等价物是什么?

Spi*_*ams 128 android android-layout android-tablelayout

我在Android中使用TableLayout.现在我有一个TableRow,里面有两个项目,在它下面是一个TableRow,它有一个项目.它呈现如下:

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|   Cell 3    |
---------------
Run Code Online (Sandbox Code Playgroud)

我想要做的是让Cell 3延伸到两个上部单元格,所以它看起来像这样:

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|           Cell 3          |
-----------------------------
Run Code Online (Sandbox Code Playgroud)

在HTML中我会使用COLSPAN ....如何在Android中使用它?

Sep*_*phy 189

似乎有一个属性在做: layout_span

更新:此属性必须应用于TableRow的子项.不是TableRow本身.

  • 是的,这是一个.但是,Eclipse布局小部件似乎并不了解它,因为它没有在可用属性中列出.但它确实有效. (8认同)
  • 您知道,答案中的更新为+1.我试图找出为什么Eclipse没有在Ctrl + Space上提供该选项!:D;) (2认同)

Mar*_*ues 91

只是为了完成答案,必须将layout_span属性添加到子项,而不是TableRow.

这个片段显示了我的tableLayout的第三行,它跨越了2列.

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="@string/create" />
    </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)


Oni*_*sha 34

这就是你以编程方式进行的方式

//theChild in this case is the child of TableRow
TableRow.LayoutParams params = (TableRow.LayoutParams) theChild.getLayoutParams();
params.span = 2; //amount of columns you will span
theChild.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

  • 您可能还需要设置params.weight = 1以使跨区内容填充该行. (6认同)
  • 确保首先将子项添加到行中. (6认同)

Rag*_*dra 26

您必须使用layout_weight来填充整行,否则它仍然填充表格布局的左列或右列.

<TableRow
  android:id="@+id/tableRow1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:layout_weight="1"
            android:text="ClickMe" />

    </TableRow>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.对于小型或灵活的元素,这是缺少的信息,在我的例子中是Spinner. (3认同)

Dim*_*oid 5

也许这会对某人有所帮助.我尝试了解决方案,layout_span但这不适合我.所以我用这个技巧解决了这个问题.只需用LinearLayout它代替TableRow你需要colspan的地方,就是这样.