在 TableLayout 中以编程方式设置列数

发布于 2024-08-30 23:48:55 字数 273 浏览 0 评论 0原文

我有一个 XML 布局,其中包含一个具有未知数量的 TableRows 的 TableLayout... 行数将在运行时建立,但我所知道的是我想要两列...... 所以我对此有几个问题:

    - is there a way to set the whole TableLayout to have 2 columns ?

- 有没有一种方法以编程方式为(在运行时)创建的 TableRows 提供一个 id,该 id 将放置在 TableLayout 中,以便我稍后可以从软件的其他部分引用它们?

I have an XML layout which contains a TableLayout with an unknown number of TableRows...
The number of Rows will be established durin runtime, what I do know though is that I want two columns...
So I have a couple of questions regarding this :

    - is there a way to set the whole TableLayout to have 2 columns ?

- is there a way programmatically to give an id to the (during runtime) created TableRows which will be placed within the TableLayout, so I can reference them later on from other parts of the software ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2024-09-06 23:48:55

您可以通过 XML 部件和 LayoutInflater 构建表行。假设您将其作为 table_cell.xml:

<TextView android:id="@+id/text"
    android:text="woot" />

将其作为 table_row.xml(除非您正在对 TableRow 进行一些奇特的操作,否则您可能不需要将其放入它自己的 XML 文件中,而只需以编程方式创建它。结果将是相同的):

<TableRow />

假设您的 TableLayout 引用被称为“table”,您可以执行以下操作:

TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflater = getLayoutInflater();

for(int i = 0; i < 5; i++) {
    TableRow row = (TableRow)inflater.inflate(R.layout.table_row, table, false);
    View v = (View)inflater.inflate(R.layout.table_cell, row, false);
    row.addView(v);
    v = (View)inflater.inflate(R.layout.table_cell, row, false);
    row.addView(v);

    // you can store your reference to `row` here for later use
    table.addView(row);
}

使用此技术,您仍然可以在 XML 中设置布局,从而更容易阅读/组织/编辑,并且您仍然可以通过编程控制表中有多少列和行。您还可以存储对每个表行的引用以供以后使用。

You can build your table rows via XML parts and LayoutInflater. Say you had this as your table_cell.xml:

<TextView android:id="@+id/text"
    android:text="woot" />

And this as your table_row.xml (unless you're doing something fancy with your TableRow, you may not need to put it in it's own XML file, and instead just create it programmatically. The result will be the same):

<TableRow />

Assuming your TableLayout reference was called "table", you could do something like this:

TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflater = getLayoutInflater();

for(int i = 0; i < 5; i++) {
    TableRow row = (TableRow)inflater.inflate(R.layout.table_row, table, false);
    View v = (View)inflater.inflate(R.layout.table_cell, row, false);
    row.addView(v);
    v = (View)inflater.inflate(R.layout.table_cell, row, false);
    row.addView(v);

    // you can store your reference to `row` here for later use
    table.addView(row);
}

With this technique, you can still set up your layout in XML, making it easier to read/organize/edit, and you still have programmatic control over how many columns and rows are in the table. You can also store references to each table row for later use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文