0

我想知道是否可以将数据行动态添加到已经定义布局的表中......例如:

<TableLayout 
        android:id="@+id/myTableOrders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp">

            <TableRow
                android:id="@+id/ordersRow"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView android:id="@+id/name"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:layout_marginLeft="10sp"
                    android:layout_marginTop="5sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.6"             
                />                                      

                <TextView android:id="@+id/quantity"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:layout_marginTop="5sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"
                />

                <TextView android:id="@+id/total_price"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:layout_marginTop="5sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"
                />
            </TableRow>
</TableLayout>

考虑这张表,其中每一行都有这 3 个文本视图,具有该样式...我现在可以为每个文本视图提供数据吗?...或者我是否也应该在处理人口的 Java 代码中编写样式?

如果是的话..也许你可以给我一个提示:)

编辑:使用下面的代码以编程方式解决了这个问题

TableLayout ordersTable = (TableLayout)findViewById(R.id.myTableOrders);
    for (int i=0;i<products.size();i++){

        //Create a new row to be added.
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        //Create text views to be added to the row.
        TextView name = new TextView(this);
        name.setText(products.get(i).getName());
        name.setTextSize(16);
        name.setTypeface(Typeface.DEFAULT_BOLD);
        TableRow.LayoutParams nameParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
        nameParams.setMargins(7, 5, 0, 0);

        TextView quantity = new TextView(this);
        quantity.setText(products.get(i).getQuantityString());
        quantity.setTextSize(16);
        quantity.setTypeface(Typeface.DEFAULT_BOLD);
        TableRow.LayoutParams quantityParams = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f);
        quantityParams.setMargins(0, 5, 0, 0);

        TextView totalPrice = new TextView(this);
        totalPrice.setTextSize(16);
        totalPrice.setTypeface(Typeface.DEFAULT_BOLD);
        TableRow.LayoutParams totalPriceParams = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f);
        totalPriceParams.setMargins(0, 5, 0, 0);

        tr.addView(name,nameParams);
        tr.addView(quantity,quantityParams);
        tr.addView(totalPrice,totalPriceParams);

        ordersTable.addView(tr);
4

1 回答 1

0

在您的活动中,使用类似的东西

  TextView tv = (TextView) findViewById(R.id.name); //do this for all text views : retrieve them using Ids.
  tv.setText("someText") //someText = text that you want to set
于 2013-04-17T16:23:05.930 回答