代码之家  ›  专栏  ›  技术社区  ›  Spike Williams

什么是android tablelayout中“colspan”的等价物?

  •  125
  • Spike Williams  · 技术社区  · 16 年前

    我在android中使用tablelayout。现在我有一个表行,其中有两个项目,下面是一个表行,其中有一个项目。它呈现如下:

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

    我要做的是使单元格3横跨两个上部单元格,如下所示:

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

    在html中,我会使用colspan….如何在android中实现此功能?

    8 回复  |  直到 8 年前
        1
  •  186
  •   catalyst294    13 年前

    似乎有这样一个属性: layout_span

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

        2
  •  86
  •   Maragues    13 年前

    为了完成回答,layout_-span属性必须添加到子级,而不是tablerow。

    此代码段显示了TableLayout的第三行,它跨越了两列。

    <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
  •  33
  •   Onimusha    8 年前

    这就是你如何以编程的方式

    //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);
    
        4
  •  24
  •   Raghavendra    11 年前

    必须使用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>
    
        5
  •  5
  •   Dimanoid    13 年前

    也许这能帮上忙。我试着用 layout_span 但这对我不起作用。所以我用这个技巧解决了这个问题。只使用 LinearLayout 代替 TableRow 在你需要科尔斯潘的地方,就这样。

        6
  •  3
  •   Mosiur    12 年前

    在tablerow元素的子元素中使用android:layout_-span

        7
  •  1
  •   Peter    9 年前

    我对rowspan有一些问题,比如tablerow、textview等等,都是用代码生成的。即使只有一个很好的答案,它也不适用于生成的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
  •  0
  •   RobGThai    16 年前

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

    我仍然觉得很难在android中将界面分割成50-50部分。