【问题标题】:Adding TableRow Dynamically to TableLayout in xml将 TableRow 动态添加到 xml 中的 TableLayout
【发布时间】:2017-03-07 18:03:08
【问题描述】:

虽然这看起来很多重复的问题,但对我来说是第一次。找遍了,没找到结果,就发到这里了。

我正在动态创建一个表,其中TableLayout 部分用xml 部分编写。

<RelativeLayout
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        android:id="@+id/componentA">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:id="@+id/tl_componentA">


        </TableLayout>

</RelativeLayout>

我为表格创建了一个对象

TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);

现在我尝试从这里开始动态添加一行

createTableRowColumn(tableLayoutA);

相关的功能是

private void createTableRowColumn(TableLayout tableLayout){

        TableRow tableRow1= new TableRow(this.context);
        tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        setTableRowColumnProperty(tableRow1);
        tableLayout.addView(tableRow1);
    }

private void setTableRowColumnProperty(TableRow tableRow){

    TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
    tableRow.setLayoutParams(layoutParams);
}

我做了所有这些,但模拟器中没有任何显示。但是当我手动在 xml 中给出相同的结构时,事情就很好了。

出于这个原因,我试图弄清楚

Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();

在 toast 中它显示为 0。我不明白为什么,尽管我已在 xml 本身中修复了 tableLayoutA 的大小。

【问题讨论】:

  • 您能详细说明一下吗?
  • 无论如何我理解动态,当数据在您的 TableLayout 中不断变化时,这就是您想说的吗?或
  • 能否请您更正代码并将其发布,以便我可以找到我做错的地方?
  • 是的,可以随时删除添加的行。
  • Mike.. 那根本没有效果

标签: android tablelayout android-tablelayout


【解决方案1】:

现实生活中的表格至少需要一行一列。 您没有看到表格,因为您只创建了一行,但是没有列。

您需要在行中添加一列才能显示某些内容。

试试这个:

  TextView label_date = new TextView(this);
    label_date.setText("DATE");
    label_date.setTextColor(Color.WHITE);
    label_date.setPadding(5, 5, 5, 5);
    tableRow.addView(label_date);// add the column to the table row here

    tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

您可以将 textView 替换为您想要的任何值。

tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

这里发生的事情是,你说,在 tableLayout 中添加一个视图,然后告诉它是哪个视图,你还说视图的宽度和高度应该包裹它的内容。

另外,指定 40,70 也不是一个好主意,当您有不同的屏幕尺寸时会发生什么。此外,使用库来处理动态视图的添加和删除。您无需重新发明轮子并节省大量时间和精力。对于表视图,https://github.com/EsotericSoftware/tablelayout 可能是一个不错的(不确定)。另一个问题是,表视图是您要找的吗?确保您没有将 recyclerView 误认为是表视图,我这样说是因为我不了解您的用例。

有用的链接:https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/

【讨论】:

  • 那行得通。你能解释一下发生了什么吗?还要解释这是如何工作的 --- tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  • 你为什么不使用 TableRow.LayoutParams?
  • 我想将行大小固定为 (70*40)dp 我该怎么做?
  • 1) 为什么不使用 TableRow.LayoutParams? :因为它似乎不起作用。它可能起作用,我不知道,但我做了什么,确实起作用了。 2) 我想将行大小固定为 (70*40)dp 我该怎么做?您需要将表格宽度和高度设置为 70*40,将行宽和高度设置为匹配父项,将列高和宽度设置为包装内容(或匹配父项)和匹配父项。
  • 这两种方式都不起作用。所以安装了半动态布局,我把它做成了完整的动态布局,现在一切都在工作,我之前尝试过但当时没有工作.. :-/
猜你喜欢
  • 1970-01-01
  • 2018-09-19
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 2018-11-07
  • 2012-12-17
相关资源
最近更新 更多