【问题标题】:Android TableLayout - span element across two rowsAndroid TableLayout - 跨两行的跨元素
【发布时间】:2017-04-17 23:25:44
【问题描述】:

我正在使用 TableLayout 来定位一些按钮,我想将按钮 2 跨越 2 行,所以它会在按钮 1,3 旁边。

我知道它可以使用网格/相对布局来完成,但我需要让它与表格布局一起使用。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


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

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
    </TableRow>

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

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />

    </TableRow>

</TableLayout>

【问题讨论】:

  • 你有什么解决办法吗?
  • 你不能直接使用 TableLayout 来做,你可以取一行并给它两倍的权重,而不是在那里插入另一个布局,给你想要的东西。

标签: android tablelayout


【解决方案1】:

您可以使用 TableLayout 中的 TableLayout 跨越 2 行。检查我的代码:

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

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <Button
            android:layout_gravity="center"
            android:text="Column 1" />

        <TableLayout>

            <TableRow>

                <Button
                    android:layout_gravity="center"
                    android:text="Column 2" />
            </TableRow>

            <TableRow>

                <Button
                    android:layout_gravity="center"
                    android:text="Column 2" />
            </TableRow>
        </TableLayout>
    </TableRow>

</TableLayout>

【讨论】:

  • 我的想法是帮助我,+1,不过,我不使用你的布局。但是用你的想法谢谢。
  • 一个线性布局在一个表格行里面,可以实现很多。
【解决方案2】:

我还没有现场测试过,但是我有类似的代码在表格布局中执行相同的操作,所以很确定如果你将按钮 1 和按钮 2 包装在 LinearLayout 中(在 TableRow1 中),你可以做到这一点:

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

    <LinearLayout android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">

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

    </LinearLayout>

   </TableRow>

希望这会有所帮助。

【讨论】:

  • 我的应用程序有几个按钮,使用这种方法会创建一个非常混乱的布局,我必须封装我的表格布局的 2 行才能达到这个结果。
  • 了解(毕竟那是现实生活),但没有具体说明,所以我提供了我能做到的最好/最简单/最快的答案。
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多