“colspan”相当于什么?在 Android TableLayout 中?

发布于 2024-08-30 23:13:23 字数 516 浏览 10 评论 0原文

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

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

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

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

在 HTML 中,我会使用 COLSPAN.... 如何在 Android 中实现此功能?

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:

-----------------------------
|   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 1    |  Cell 2     |
-----------------------------
|           Cell 3          |
-----------------------------

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

趁微风不噪 2024-09-06 23:13:23

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

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

It seems that there is an attribute doing that :
layout_span

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

情痴 2024-09-06 23:13:23

为了完成答案,必须将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>

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

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

<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>
眼泪都笑了 2024-09-06 23:13:23

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

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

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);
绝情姑娘 2024-09-06 23:13:23

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

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

<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>
世俗缘 2024-09-06 23:13:23

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

Maybe this will help someone. I tried the solution with layout_span but this not working for me. So I solved the problem with this trick. Just use LinearLayout in place of TableRow where you need colspan, that's all.

木落 2024-09-06 23:13:23

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

use android:layout_span in child element of TableRow element

荭秂 2024-09-06 23:13:23

我在使用代码生成的 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 内。在这种情况下,它有效。
希望这会对某人有所帮助!

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.

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.

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!

小镇女孩 2024-09-06 23:13:23

实际上它非常简单。这是我以编程方式解决的方案

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

Actually It is pretty straight forward. This is my solution programmatically

        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);
枯叶蝶 2024-09-06 23:13:23

我认为你需要将一个布局包裹在另一个布局周围。
垂直有一个布局列表,内部有另一个(或在本例中为两个)水平列表。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文