android - 在 Android 中以编程方式添加 TableRow

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

我想以编程方式添加这个 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);
}

最佳答案

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

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

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

首先,创建(或保留)包含您的TableLayoutXML 文件。其次,创建一个单独的 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 之前执行此操作。如果您希望列标题基本上包含代码、说明、金额和指标,请像您目前那样在 TableLayout 中创建一个 TableRow。以编程方式创建的 TableRows 将排在该“标题”行之后。

关于android - 在 Android 中以编程方式添加 TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31929511/

相关文章:

android - 在导航 Controller 上添加 fragment 而不是替换 fragment ?

android - 工具栏标题在 fragment 中显示为空

android - 文本到语音 setLanguage() 不起作用

android - dex 在 Android 上为 Scala 构建 Ant 时失败

Android自定义控件这可能吗?

android - 添加页脚的最佳方法是什么,无论屏幕分辨率如何,页脚都位于底部?

java - 如何实现这个 View

android - 在 RelativeLayout 中设置 minHeight,禁用 alignBaseline?

java - 如何判断Timer任务是否完成

Android - 来自支持库的 fragment 与 4.3 平台不兼容