簡體   English   中英

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

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

我在 Android 中使用 TableLayout。 現在我有一個包含兩個項目的 TableRow,在它下面是一個包含一個項目的 TableRow。 它呈現如下:

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

我想要做的是讓 Cell 3 橫跨兩個上部單元格,所以它看起來像這樣:

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

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

似乎有一個屬性在做: layout_span

更新:此屬性必須應用於TableRow的子項。 不是TableRow本身。

只是為了完成答案,必須將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>

這就是你以編程方式進行的方式

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

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

也許這會對某人有所幫助。 我嘗試使用layout_span解決方案,但這對我不起作用。 所以我用這個技巧解決了這個問題。 只需使用LinearLayout代替TableRow ,您需要colspan,這就是全部。

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

對於使用代碼生成的TableRow,Textview等情況,我遇到了一些rowpan問題。 即使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。 在這種情況下,它的工作原理。 希望這會對某人有所幫助!

我認為你需要圍繞另一個布局。 一個布局列表垂直,里面有另一個(或在這種情況下,兩個)水平列表。

我仍然覺得很難將界面很好地拆分為Android中的50-50部分。

實際上這是非常簡單的。 這是我以編程方式的解決方案

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM