Android 以编程方式创建 TableLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:12:40  来源:igfitidea点击:

Create TableLayout programmatically

androidtablelayout

提问by mark

I'm trying to create a TableLayout programatically. It just won't work. The same layout in an xml file works though. This is what I have:

我正在尝试以编程方式创建一个 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);

When I run this, I don't get a crash, but nothing appears. If I get rid of the TableRow instance, at least the button does appear as a direct child of the TableLayout. What am I doing wrong?

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

回答by Grigori A.

Just to make the answer more clear:

只是为了让答案更清楚:

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);

Explanation
When you call setLayoutParams, you are supposed to pass the LayoutParamsof the parent view

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

回答by mark

It turns out I needed to specify TableRowLayout, TableLayout etc for the layout params, otherwise the table just won't show!

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

回答by clotilde

A good solution is to inflate layout files for each instance of row you want to create. See this post : How to duplicate Views to populate lists and tables?

一个好的解决方案是为要创建的每个行实例扩充布局文件。请参阅这篇文章:如何复制视图以填充列表和表格?

回答by ericn

Your problem is at this line:

你的问题是在这一行:

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

You need to change LayoutParamsto TableRow.LayoutParams:

您需要更改LayoutParamsTableRow.LayoutParams

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

回答by John Moses

For me, to get mine I had to call addContentView().

对我来说,为了得到我的,我不得不打电话addContentView()