【问题标题】:Adding TableRow programmatically in Android在 Android 中以编程方式添加 TableRow
【发布时间】:2015-08-10 21:29:25
【问题描述】:

我正在使用 Android 应用程序,我想以编程方式在我的 TableLayout 中添加一个 TableRow。

我有这个表格布局:

<TableLayout
android:id="@+id/details_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TableRow>
    <TextView
        android:text="4686"
        android:layout_width="wrap_content"
        android:layout_column="0"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />
    <TextView
        android:text="sdhiuf osdfh isdhf ihdf"
        android:layout_width="wrap_content"
        android:layout_column="1"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />
    <TextView
        android:text="2"
        android:layout_width="wrap_content"
        android:layout_column="2"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black" />
    <TextView
        android:text="UN"
        android:layout_width="wrap_content"
        android:layout_column="3"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black" />
</TableRow>

我想以编程方式准确地添加 TableRow。

我正在尝试这样的事情:

TableLayout detailsTable = (TableLayout) l.findViewById(R.id.details_table);

for(Nfce_Product nfceProduct : nfceProducts){
    TableRow tableRow = new TableRow(getActivity());

    TextView tvProductCode = new TextView(getActivity());
    tvProductCode.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductCode.setText(nfceProduct.getProduct_code());
    tvProductCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductCode.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductDescription = new TextView(getActivity());
    tvProductDescription.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductDescription.setText(nfceProduct.getProduct_description());
    tvProductDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductDescription.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductAmount = new TextView(getActivity());
    tvProductAmount.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductAmount.setText(String.valueOf(nfceProduct.getAmount()));
    tvProductAmount.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductAmount.setTextColor(getResources().getColor(R.color.black));

    TextView tvProductMetric = new TextView(getActivity());
    tvProductMetric.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
    tvProductMetric.setText(nfceProduct.getProduct_metric());
    tvProductMetric.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
    tvProductMetric.setTextColor(getResources().getColor(R.color.black));

    tableRow.addView(tvProductCode);
    tableRow.addView(tvProductDescription);
    tableRow.addView(tvProductAmount);
    tableRow.addView(tvProductMetric);
    detailsTable.addView(tableRow);
}

【问题讨论】:

  • 另外,您需要为每个文本视图设置一个布局高度。
  • 你看到了什么错误?
  • tableRow 只是没有显示出来
  • 我们能看到更多你的代码吗?比如你如何声明表格以及将表格放置在布局中的 xml?你确定桌子出现了吗?
  • 您没有包括所有内容,想知道您从哪里获得详细信息表?那是来自 findviewbyid 或类似的还是以编程方式创建的?另外,假设产品 detailsTextSize 是 7?

标签: android android-layout android-fragments android-tablayout


【解决方案1】:

Here is my own answer 我自己动态创建的TableRow 问题。我认为我的回答已经足够详细了,你应该把它当作自己的没有问题!我的问题不仅涉及动态创建TableRows,还涉及能够触摸每一行并发生一些事情。我现在比这更进一步,让每个 CELL 都可以单独点击。

编辑:您需要有一个单独的 TableRow XML 文件可以访问,与您尝试动态填充的实际 TableLayout 分开。

Edit2:我可能应该尝试为您制定一个实际的解决方案,以便您了解我在说什么:

首先,创建(或保留)您的 XML 文件,其中包含您的 TableLayout。其次,创建一个单独的TableRowXML文件。

(这是你潜在的 tablerow.xml 文件)

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:focusable="true" >
    <TextView
        android:id="@+id/tableCell1"
        android:layout_width="wrap_content"
        android:layout_column="0"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />

    <TextView
        android:id="@+id/tableCell2"
        android:layout_width="wrap_content"
        android:layout_column="1"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />

    <TextView
        android:id="@+id/tableCell3"
        android:layout_width="wrap_content"
        android:layout_column="2"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />

    <TextView
        android:id="@+id/tableCell4"
        android:layout_width="wrap_content"
        android:layout_column="3"
        android:layout_weight="1"
        android:textSize="7px"
        android:textColor="@color/black"  />
</TableRow>

现在...回到您的实际代码!

for (Nfce_Product nfceProduct : nfceProducts) {
    final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
    final TableRow tableRow = (TableRow) getLayoutInflater().inflate(R.layout.tablerow, null);
    TextView tv;

    //Filling in cells
    tv = (TextView) tableRow.findViewById(R.id.tableCell1);
    tv.setText(nfceProduct.getProduct_code());

    tv = (TextView) tableRow.findViewById(R.id.tableCell2);
    tv.setText(nfceProduct.getProduct_description());

    tv = (TextView) tableRow.findViewById(R.id.tableCell3);
    tv.setText(nfceProduct.getAmount());

    tv = (TextView) tableRow.findViewById(R.id.tableCell4);
    tv.setText(nfceProduct.getProduct_metric());

    //Add row to the table
    detailsTable.addView(tableRow);
} //End for

如果您仍然需要/想要更改文本大小和颜色,可以在 setText() 行之后和再次进行 findViewById 之前进行。如果您希望列标题基本上是代码、描述、数量和度量,请像现在一样在TableLayout 内创建一个TableRow。以编程方式创建的TableRows 将排在该“标题”行之后。

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多