因此,我在 XML 中声明了一个 TableLayout,其中第一行有 2 列,第二行有 4 列。
--------- ---------
| | |
| A | B |
| | |
--------- ---------
| | | | |
| | | | |
--------- ---------
现在我想动态更改第一行中第二列(B 列)的列跨度以跨越整个 4 列而不是两列,并且第一列(A 列)消失,并且能够切换回两列状态。因此,表格布局的第二个状态将是这样的:
-------------------
| |
| B |
| |
--------- ---------
| | | | |
| | | | |
--------- ---------
最初,A 和 B 都是在 XML 中创建的,加载后调整大小并添加边距:
TableRow.LayoutParams bLp = new TableRow.LayoutParams(size, size);
bLp.setMargins(margin, 0, 0, margin);
bLp.span = 2;
b.setLayoutParams(bLp);
但是如果我尝试更改第二列的跨度并隐藏第一列,唯一改变的是第二列向左滑动
a.setVisibility(View.GONE);
TableRow.LayoutParams bLp = (TableRow.LayoutParams) b.getLayoutParams();
bLp.span = 4;
b.setLayoutParams(bLp);
将第二列的宽度更改为 MATCH_PARENT 也没有帮助,但正如 TableRow.LayoutParams 的 Android 文档所说,根本不考虑宽度:
This set of layout parameters enforces the width of each child to be MATCH_PARENT and the height of each child to be WRAP_CONTENT, but only if the height is not specified.
有没有办法实现我想要用 TableLayouts 做的事情?
谢谢
最佳答案
现在明白了,当隐藏“A”列时,我需要将其跨度设置为 0,另外将 B 的跨度设置为 4,因此整个代码将是:
TableRow.LayoutParams aLp = (TableRow.LayoutParams) a.getLayoutParams();
aLp.span = 0;
a.setLayoutParams(aLp);
a.setVisibility(View.GONE);
TableRow.LayoutParams bLp = (TableRow.LayoutParams) b.getLayoutParams();
bLp.span = 4;
b.setLayoutParams(bLp);
关于java - 更改 Android TableLayout 列跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21004595/