【问题标题】:Extending TableLayout class - adding rows扩展 TableLayout 类 - 添加行
【发布时间】:2011-01-24 15:17:54
【问题描述】:

我正在尝试扩展 TableLayout 类,以便该类自动填充行。我遇到的问题是我在自定义类中添加的行不显示。

例如。活动内:

private TextView getTableCell(String text) {
    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    return tv;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table);
    TableRow tr = new TableRow(this);
    tr.addView(getTableCell("activity1"));
    tr.addView(getTableCell("activity2"));
    tr.addView(getTableCell("activity3"));
    table.addView(tr);

这成功地向表中添加了一行。但是,在我的自定义类中:

private TextView getTableCell(String text) {
    TextView tv = new TextView(context);
    tv.setText(text);
    tv.setLayoutParams(new LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));

    return tv;
}

private void update() {
    TableRow tr = new TableRow(context);
    tr.addView(getTableCell("class1"));
    tr.addView(getTableCell("class2"));
    tr.addView(getTableCell("class3"));
    addView(tr);
}

没有成功添加一行。好吧,它确实添加了一行,因为 getChildCount() 和 getChildAt() 确实返回了这一行 - 但它没有显示出来。

我错过了什么吗?

【问题讨论】:

  • 我发现了问题。在 DatasetTableLayout.getTableCell()(DatasetTableLayout 扩展 TableLayout)中,调用 new LayoutParams() 创建了一个 android.widget.TableLayout.LayoutParams 对象,而我需要的是一个 android.widget.TableRow.LayoutParams。经验教训 - 不要依赖 Eclipse 的组织导入命令!!
  • 它救了我的命。你说得对,任何 layoutparam 都不能在 tablelayout 上工作,但是 talberow.layoutparam

标签: android android-widget android-layout


【解决方案1】:

@theresia(如果我添加评论,似乎无法格式化文本): 它似乎确实添加了 TableRow:

for(int i = 0; i < getChildCount(); i++) {
    TableRow row = (TableRow) getChildAt(i);
    for(int j = 0; j < row.getChildCount(); j++) {
        TextView tv = (TextView) row.getChildAt(j);
        Log.e("DatasetTableLayout-data", tv.getText().toString() + " ");
    }
}

给我:

01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1 
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2 
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3 

【讨论】:

    【解决方案2】:

    我的猜测是这样的:addView(tr); 它是否成功地将您的TableRow 添加到TableLayout

    编辑:

    TableLayout tb = new TableLayout(this);
    TableRow tr = new TableRow(this);
    TextView tv = new TextView(this);
    tv.setText("row");
    tr.addView(tv);
    tb.addView(tr); // this is what I mean
    setContentView(tb);
    

    您用于创建行的代码工作正常,但如果您没有将其添加到表中,并且稍后将表添加到父视图中,您的行将不会显示。

    【讨论】:

    • 在下面查看我的答案(如果我添加评论,似乎无法正确格式化文本)
    • 您可以编辑您的原始帖子。无论如何,我的意思是:你调用了 addView(tr),但是在哪里添加呢?也许这就是它没有显示的原因。您应该获得对要更新的行的引用,然后调用 addView(tr)。
    • 已通过 addView(tr) 成功添加到表中; update() 中的语句。问题出在 LayoutParams 上(请参阅原始帖子的评论)。
    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2019-09-13
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多