在Android中以编程方式添加TableRow

Hen*_*ulo 2 android android-layout android-fragments android-tablayout

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

我有这个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>
Run Code Online (Sandbox Code Playgroud)

我想以编程方式添加这个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);
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*awg 5

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

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

编辑2:我应该尝试为你做一个实际的解决方案,以便你看到我在说什么:

首先,创建(或保留)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>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

如果您仍然需要/想要更改文本大小和颜色,可以setText()在行之后和findViewById再次之前执行此操作.如果您希望列标题基本上代码为"代码","描述","金额"和"度量标准",请将其设置为当前TableRow内部TableLayout.以TableRows编程方式创建的将在该"标题"行之后排成一行.