将行动态添加到设计布局的表中

时间:2013-04-17 15:44:24

标签: android dynamic tablelayout

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

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

1 个答案:

答案 0 :(得分: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