【问题标题】:What is the equivalent of "colspan" in an Android TableLayout?Android TableLayout 中的“colspan”等价物是什么?
【发布时间】:2010-04-26 02:24:05
【问题描述】:

我在 Android 中使用 TableLayout。现在我有一个包含两个项目的 TableRow,在它下面,一个包含一个项目的 TableRow。它呈现如下:

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|   Cell 3    |
---------------

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

-----------------------------
|   Cell 1    |  Cell 2     |
-----------------------------
|           Cell 3          |
-----------------------------

在 HTML 中,我会使用 COLSPAN....如何在 Android 中进行这项工作?

【问题讨论】:

  • 恕我直言,我找到了更好的方法来做到这一点。答案在这里:stackoverflow.com/a/18682293/1979882
  • 作为记录,请注意 TableLayout 本质上是一个 LinearLayout。这意味着您可以直接添加任何孩子。因此,对于单个全列跨度视图,您可以跳过 TableRow。

标签: android android-layout android-tablelayout


【解决方案1】:

似乎有一个属性可以做到这一点: layout_span

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

【讨论】:

  • 是的,就是这个。然而,Eclipse 布局小部件似乎并不知道它,因为它没有在可用属性中列出。但它确实有效。
  • 您知道,+1 表示您在答案中的更新。我试图找出为什么 Eclipse 没有在 Ctrl+Space 上提供该选项! :D ;)
  • 我在视图上使用 layout_span。然后我不能在那个视图上使用权重。我必须使用 wrap_content。为什么?
【解决方案2】:

只是为了完成答案,layout_span属性必须添加到child,而不是TableRow。

这个 sn-p 显示了我的 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>

【讨论】:

    【解决方案3】:

    这就是您以编程方式执行此操作的方式

    //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);
    

    【讨论】:

    • 您可能还需要设置 params.weight = 1 以使跨越的内容填满行。
    • 它不起作用。 stackoverflow.com/a/18682293/1979882这里写了解释。
    • 确保先将孩子添加到行中。
    • @DacSaunders 回答回滚。您的编辑特定于您的需要。最初的答案是该方法的通用答案,31 个竖起大拇指可能会告诉您您可能做错了什么?另请参阅 Artem 的评论,强调需要先添加孩子。在这种情况下,child 可以是任何东西,而不仅仅是您的编辑建议的 textview。谢谢
    【解决方案4】:

    您必须使用 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>
    

    【讨论】:

    • 谢谢。这是小型或灵活元素的缺失信息,在我的例子中是 Spinner。
    【解决方案5】:

    也许这会对某人有所帮助。我尝试了layout_span 的解决方案,但这对我不起作用。所以我用这个技巧解决了这个问题。只需在需要 colspan 的地方使用 LinearLayout 代替 TableRow 即可。

    【讨论】:

    • 而且它不需要是LinearLayout。我只是按照我的意愿放了一个视图。
    【解决方案6】:

    在 TableRow 元素的子元素中使用 android:layout_span

    【讨论】:

      【解决方案7】:

      在使用代码生成的 TableRow、Textview 等的情况下,我遇到了一些行跨度问题。即使 Onimush 的答案看起来不错,它也不适用于生成的 UI。

      这是一段代码......不起作用:

                  TableRow the_ligne_unidade = new TableRow(this);
                  the_ligne_unidade.setBackgroundColor(the_grey);
      
                  TextView my_unidade = new TextView(this);
                  my_unidade.setText(tsap_unidade_nom);
                  my_unidade.setTextSize(20);
                  my_unidade.setTypeface(null, Typeface.BOLD);
                  my_unidade.setVisibility(View.VISIBLE);
      
                  TableRow.LayoutParams the_param;
                  the_param = (TableRow.LayoutParams)my_unidade.getLayoutParams();
                  the_param.span = 3;
                  my_unidade.setLayoutParams(the_param);
      
                  // Put the TextView in the TableRow
                  the_ligne_unidade.addView(my_unidade);
      

      代码似乎没问题,但是当您到达“the_params”的 init 时,它返回 NULL。

      另一方面,这段代码就像一个魅力:

                  TableRow the_ligne_unidade = new TableRow(this);
                  the_ligne_unidade.setBackgroundColor(the_grey);
      
                  TextView my_unidade = new TextView(this);
                  my_unidade.setText(tsap_unidade_nom);
                  my_unidade.setTextSize(20);
                  my_unidade.setTypeface(null, Typeface.BOLD);
                  my_unidade.setVisibility(View.VISIBLE);
      
                  // Put the TextView in the TableRow
                  the_ligne_unidade.addView(my_unidade);
      
                  // And now, we change the SPAN
                  TableRow.LayoutParams the_param;
                  the_param = (TableRow.LayoutParams)my_unidade.getLayoutParams();
                  the_param.span = 3;
                  my_unidade.setLayoutParams(the_param);
      

      唯一的区别是我在设置跨度之前将 Textview 推到 TableRow 中。在这种情况下,它可以工作。 希望这会对某人有所帮助!

      【讨论】:

        【解决方案8】:

        其实这很简单。这是我的编程解决方案

                TableLayout tableLayout = binding.tableLayout;
                TableRow row = new TableRow(this);
                TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
                layoutParams.span = 4;               // define no. of column span will row do
                row.setLayoutParams(layoutParams);
        
                TextView noDataTextView = new TextView(this);
                noDataTextView.setText("No Data");
                noDataTextView.setGravity(Gravity.CENTER);
                noDataTextView.setLayoutParams(layoutParams);   //This line will span your row
        
                row.addView(noDataTextView);
                tableLayout.addView(row, 1);
        

        【讨论】:

          【解决方案9】:

          我认为您需要将布局包裹在另一个布局上。 有一个垂直布局列表,里面有另一个(或者在本例中是两个)水平列表。

          我仍然发现很难在 Android 中很好地将界面拆分为 50-50 部分。

          【讨论】:

          • 我最终这样做是为了解决另一个与表格相关的错误,该错误导致我的布局的 1/3 在横向模式下隐藏,原因不明。
          猜你喜欢
          • 2021-12-03
          • 1970-01-01
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 2013-03-12
          • 1970-01-01
          • 2012-08-30
          相关资源
          最近更新 更多