我有一个TableLayout
与多个TableRow
里面的景色。我希望以编程方式指定行的高度。例如。
int rowHeight = calculateRowHeight();
TableLayout tableLayout = new TableLayout(activity);
TableRow tableRow = buildTableRow();
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, rowHeight);
tableLayout.addView(tableRow, rowLp);
但这不起作用,并且默认为 WRAP_CONTENT。四处挖掘安卓源代码 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TableLayout.java,我看到这个TableLayout
(由onMeasure()方法触发):
private void findLargestCells(int widthMeasureSpec) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child instanceof TableRow) {
final TableRow row = (TableRow) child;
// forces the row's height
final ViewGroup.LayoutParams layoutParams = row.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
似乎任何设置行高的尝试都会被 TableLayout 覆盖。有人知道解决这个问题的方法吗?
好吧,我想我现在已经掌握了窍门。设置行高的方法不是摆弄TableLayout.LayoutParams
附于TableRow
,但是TableRow.LayoutParams
附于any的细胞。只需将一个单元格设置为所需的高度(假设它是最高的单元格)整行都将是该高度。就我而言,我添加了一个额外的 1 像素宽的列,设置为所需的高度,这达到了目的:
View spacerColumn = new View(activity);
//add the new column with a width of 1 pixel and the desired height
tableRow.addView(spacerColumn, new TableRow.LayoutParams(1, rowHeight));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)