139

AndroidでTableLayoutを使用しています。現在、2つのアイテムを含む1つのTableRowがあり、その下に1つのアイテムを含むTableRowがあります。次のようにレンダリングされます。

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

私がやりたいのは、セル3を両方の上部セルにまたがって伸ばすことです。したがって、次のようになります。

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

HTMLではCOLSPANを使用します...Androidでこれを機能させるにはどうすればよいですか?

4

9 に答える 9

205

それを行う属性があるようです: layout_span

更新:この属性は、TableRowの子に適用する必要があります。TableRow自体ではありません。

于 2010-04-26T12:17:33.730 に答える
96

答えを完成させるには、layout_span属性をTableRowではなく子に追加する必要があります。

このスニペットは、2列にまたがるtableLayoutの3番目の行を示しています。

<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>
于 2012-02-22T10:03:07.480 に答える
41

そして、これはあなたがプログラムでそれを行う方法です

//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);
于 2012-10-04T09:33:24.400 に答える
27

行全体を塗りつぶすには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>
于 2014-10-23T12:29:05.317 に答える
5

多分これは誰かを助けるでしょう。私は解決策を試しましたが、これはうまくいきlayout_spanませんでした。だから私はこのトリックで問題を解決しました。colspanが必要なLinearLayout場所の代わりに使用するだけです。それだけです。TableRow

于 2012-12-28T15:35:40.543 に答える
3

TableRow要素の子要素でandroid:layout_spanを使用します

于 2013-12-16T07:39:16.540 に答える
1

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」の初期化に達すると、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);

唯一の違いは、スパンを設定する前に、TableRow内にTextviewをプッシュすることです。そしてこの場合、それは機能します。これが誰かを助けることを願っています!

于 2016-11-29T00:31:30.077 に答える
1

実際、それはかなり簡単です。これはプログラムで私の解決策です

        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);
于 2021-11-07T02:41:37.973 に答える
0

レイアウトを別のレイアウトにラップする必要があると思います。1つのレイアウトリストを垂直に、内部に別の1つ(この場合は2つ)のリストを水平に配置します。

Androidでインターフェースを50-50の部分にうまく分割するのはまだ難しいと感じています。

于 2010-04-26T04:03:17.877 に答える