Android 如何以编程方式将表行添加到表中?
我试图添加一个自定义类,将表行扩展到表中 这是自定义类: 包com.thegame.午餐Android 如何以编程方式将表行添加到表中?,android,view,tablerow,Android,View,Tablerow,我试图添加一个自定义类,将表行扩展到表中 这是自定义类: 包com.thegame.午餐 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 a
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中的所有内容。对于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));
感谢现在行显示正确,但当我点击编辑文本时,没有弹出键盘。另外,您是否考虑过使用ListView而不是动态创建TableLayout?ListView就是为这类事情而设计的。他们负责自己的子视图创建和滚动,总体上表现更好。我在应用程序的其他地方使用listview,但我没有找到一种在listview中显示列的干净方法,所以我改为使用它。你必须创建一个自定义适配器,覆盖getView方法,但是,只要您提供一个与上面创建的一样的行布局,它就会很好地工作。