2

I'm working in an Android application and I want to add a TableRow programmatically in my TableLayout.

I have this TableLayout:

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

and I want to add this exactly TableRow programmatically.

I'm trying something like this:

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

1 回答 1

7

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

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

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

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

(这是您潜在的 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。如果您希望列标题基本上是代码、描述、金额和指标,请在您当前拥有的TableRow内部创建一个。TableLayoutTableRows编程方式创建的将落在该“标题”行之后。

于 2015-08-11T13:07:12.067 回答