在 TableLayout 中以编程方式设置列数
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 XML 部件和 LayoutInflater 构建表行。假设您将其作为 table_cell.xml:
将其作为 table_row.xml(除非您正在对 TableRow 进行一些奇特的操作,否则您可能不需要将其放入它自己的 XML 文件中,而只需以编程方式创建它。结果将是相同的):
假设您的 TableLayout 引用被称为“table”,您可以执行以下操作:
使用此技术,您仍然可以在 XML 中设置布局,从而更容易阅读/组织/编辑,并且您仍然可以通过编程控制表中有多少列和行。您还可以存储对每个表行的引用以供以后使用。
You can build your table rows via XML parts and LayoutInflater. Say you had this as your table_cell.xml:
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):
Assuming your TableLayout reference was called "table", you could do something like this:
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.