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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:02:12  来源:igfitidea点击:

What is the equivalent of "colspan" in an Android TableLayout?

androidandroid-layoutandroid-tablelayout

提问by Spike Williams

I'm using a TableLayout in Android. Right now I have one TableRow with two items in it, and, below that, a TableRow with one item it it. It renders like this:

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

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

What I want to do is make Cell 3 stretch across both upper cells, so it looks like this:

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

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

In HTML I'd use a COLSPAN.... how do I make this work in Android?

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

回答by Sephy

It seems that there is an attribute doing that : layout_span

似乎有一个属性这样做: layout_span

UPDATE: This attribute must be applied to the children of the TableRow. NOT to the TableRow itself.

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

回答by Maragues

Just to complete the answer, the layout_span attribute must be added to the child, not to TableRow.

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

This snippet shows the third row of my tableLayout, which spans for 2 columns.

这个片段显示了我的 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>

回答by Onimusha

And this is how you do it programmatically

这就是你如何以编程方式做到这一点

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

回答by Raghavendra

You have to use layout_weight to fill the entire row otherwise it still fills left or right column of table layout.

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

回答by Dimanoid

Maybe this will help someone. I tried the solution with layout_spanbut this not working for me. So I solved the problem with this trick. Just use LinearLayoutin place of TableRowwhere you need colspan, that's all.

也许这会帮助某人。我尝试了解决方案,layout_span但这对我不起作用。所以我用这个技巧解决了这个问题。只需LinearLayoutTableRow需要 colspan 的地方使用,仅此而已。

回答by Mosiur

use android:layout_span in child element of TableRow element

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

回答by Peter

I've had some problem with rowspan, in case of TableRow, Textview and so on, generated with code. Even if Onimush answer seems to be good, it don't works with generated UI.

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

Here is a piece of code which.... don't work:

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

            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 code seems to be OK but, when you reach the init of "the_params" it returns NULL.

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

On the other end, this code works like a charm:

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

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

The only difference is that I push the Textview inside the TableRow before setting the span. And in this case, it works. Hope this will help someone!

唯一的区别是我在设置跨度之前将 Textview 推送到 TableRow 中。在这种情况下,它起作用了。希望这会帮助某人!

回答by RobGThai

I think you need to wrap a layout around another one. Have one Layout list vertically, inside have another one (or in this case, two) list horizontally.

我认为您需要将布局包装在另一个布局周围。垂直有一个布局列表,里面有另一个(或在这种情况下,两个)水平列表。

I'm still finding it hard to nicely split interface to 50-50 portion in Android.

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