动态地将TableRow添加到TableLayout

dfe*_*r88 19 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.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));
}
Run Code Online (Sandbox Code Playgroud)

R.id.spreadsheet是一个xml TableLayout.我可以从调试中看到正在访问该方法,但没有任何内容被绘制到屏幕上.是什么赋予了?我是否需要以某种方式重置内容视图?

dfe*_*r88 27

事实证明,Eclipse并不总是正确的.Ctrl + Shift + M导入错误.它import android.view.ViewGroup.LayoutParams应该有它的时候import android.widget.TableRow.LayoutParams

现在一切都很好.