如何以编程方式将表行添加到表中?

时间:2014-04-13 01:03:39

标签: android view tablerow

我正在尝试添加一个将表行扩展到表中的自定义类。

这是自定义类:     包com.thegame.lunchilicious;

import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.TableLayout; 
import android.widget.TableRow;
import android.widget.TextView;

public class OrderRow extends TableRow {

protected int num;
protected TextView name;
protected TextView cal;
protected TextView price;
protected EditText quantity;
protected int calories;
protected int cost;

public OrderRow(Context context, Item item) {
    super(context);
    this.name = new TextView(context);
    this.cal = new TextView(context);
    this.price = new TextView(context);
    this.quantity = new EditText(context);
    this.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    this.num = item.getId();
    this.calories = item.getCalories();
    this.cost = item.getCost();
    this.name.setText(item.getName());
    this.cal.setText(item.getCalories() + "");
    this.price.setText(item.getCost() + "");
    name.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 4f));
    cal.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    price.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    quantity.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    name.setTag("name" + num);
    cal.setTag("cal" + num);
    price.setTag("price" + num);
    quantity.setTag("quantity" + num);
    quantity.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
    this.addView(name);
    this.addView(cal);
    this.addView(price);
    this.addView(quantity);
}

public OrderRow(Context context) {
    super(context);
}

public OrderRow(Context context, AttributeSet attrs) {
    super(context, attrs);
}
}

这是我onCreateView的一部分:

TableLayout table = (TableLayout) myView.findViewById(R.id.table);
List<OrderRow> rows = new ArrayList<OrderRow>();
for (Item item : items) {

        row = new OrderRow(parentActivity, item);
        rows.add(row);
        table.addView(row);
    }

没有任何表格行显示,但视图的xml中的所有内容都会显示。

1 个答案:

答案 0 :(得分:0)

对于TableRow中的视图,您需要使用TableRow.LayoutParams而不是TableLayout.LayoutParams。像这样:

name.setLayoutParams(new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 4f));
cal.setLayoutParams(new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
price.setLayoutParams(new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
quantity.setLayoutParams(new TableRow.LayoutParams(
    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));