【问题标题】:Create TableLayout programmatically以编程方式创建 TableLayout
【发布时间】:2010-12-04 11:30:47
【问题描述】:

我正在尝试以编程方式创建 TableLayout。它只是行不通。 xml 文件中的相同布局虽然有效。这就是我所拥有的:

public class MyTable extends TableLayout
{
    public MyTable(Context context) {
        super(context);

        setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        TableRow row = new TableRow(context);
        row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        Button b = new Button(getContext());
        b.setText("hello");
        b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        row.addView(b); 
        addView(row)
    }
}

...

// In main activity:
MyTable table = new MyTable(this);
mainLayout.addView(table);

当我运行它时,我没有崩溃,但什么也没有出现。如果我摆脱 TableRow 实例,至少该按钮确实显示为 TableLayout 的直接子级。我做错了什么?

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    只是为了让答案更清楚:

    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
    
    TableLayout tableLayout = new TableLayout(context);
    tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// assuming the parent view is a LinearLayout
    
    TableRow tableRow = new TableRow(context);
    tableRow.setLayoutParams(tableParams);// TableLayout is the parent view
    
    TextView textView = new TextView(context);
    textView.setLayoutParams(rowParams);// TableRow is the parent view
    
    tableRow.addView(textView);
    

    说明
    当你调用setLayoutParams时,你应该传递父视图的LayoutParams

    【讨论】:

    • 不应该是tableRow.setLayoutParams(rowParams)吗?
    • 救生员奖授予@Gigori A. 并标记。 Gigori A. 为这个答案并标记为他自己的答案。
    • 伟大而有用的解决方案。作为旁注,您应该在将 textview 添加到行之前将行添加到表中。 tableLayout.addView(tableRow);
    • 花了一个小时试图弄清楚我做错了什么 - 结果在你的代码中你错过了将 tableRow 添加到 tableLayout: tableLayout.addView(tableRow);
    【解决方案2】:

    原来我需要为布局参数指定 TableRowLayout、TableLayout 等,否则表格将不会显示!

    【讨论】:

    • 你能发布你的代码解决方案吗?看起来你已经在上面做了。
    • 谢谢! @Atma TableRows 应该使用 TableLayout.LayoutParams,而 TableRows 中的视图应该使用 TableRow.LayoutParams。 :)
    • 很糟糕,您需要明确地执行此操作,但它对我有用,谢谢!
    • 谢谢@Brayden!这是我在 TableLayout 的子类中遗漏的小细节,它自动拥有所有 TableLayout.LayoutParams 而不是 TableRow.LayoutParams!
    【解决方案3】:

    一个好的解决方案是为您要创建的每个行实例扩展布局文件。看到这个帖子:How to duplicate Views to populate lists and tables?

    【讨论】:

      【解决方案4】:

      对我来说,要得到我的,我必须打电话给addContentView()

      【讨论】:

        【解决方案5】:

        你的问题出在这一行:

        b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        

        你需要把LayoutParams改成TableRow.LayoutParams

        b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-23
          • 2015-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多