Android 将TableRow动态添加到TableLayout

Android 将TableRow动态添加到TableLayout,android,Android,单击按钮时,将运行以下方法: public void createTableRow(View v) { TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet); TableRow tr = new TableRow(this); LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); tr.s

单击按钮时,将运行以下方法:

public void createTableRow(View v) {
  TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
  TableRow tr = new TableRow(this);
  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  tr.setLayoutParams(lp);

  TextView tvLeft = new TextView(this);
  tvLeft.setLayoutParams(lp);
  tvLeft.setBackgroundColor(Color.WHITE);
  tvLeft.setText("OMG");
  TextView tvCenter = new TextView(this);
  tvCenter.setLayoutParams(lp);
  tvCenter.setBackgroundColor(Color.WHITE);
  tvCenter.setText("It");
  TextView tvRight = new TextView(this);
  tvRight.setLayoutParams(lp);
  tvRight.setBackgroundColor(Color.WHITE);
  tvRight.setText("WORKED!!!");

  tr.addView(tvLeft);
  tr.addView(tvCenter);
  tr.addView(tvRight);

  tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}

R.id.spreadsheet
是一个xml表格布局。我可以从调试中看到该方法正在被访问,但屏幕上没有任何内容。有什么好处?我是否需要以某种方式重置内容视图?

事实证明,Eclipse并不总是正确的。Ctrl+Shift+M进行了错误的导入。当它应该导入android.widget.TableRow.LayoutParams时,它已经导入了android.view.ViewGroup.LayoutParams


现在一切正常。

我们可以设置这些视图的对齐、边距和位置吗。