【问题标题】:Dynamically add TableRow to TableLayout将 TableRow 动态添加到 TableLayout
【发布时间】:2017-11-11 18:52:58
【问题描述】:

当一个按钮被点击时,会运行下面的方法:

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 表格布局。我可以从调试中看到正在访问该方法,但屏幕上没有任何内容。是什么赋予了?我需要以某种方式重置内容视图吗?

【问题讨论】:

    标签: android


    【解决方案1】:

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

    现在一切正常。

    【讨论】:

    • 我们可以设置这些视图的对齐方式、边距和位置吗?
    猜你喜欢
    • 2018-09-19
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多