我有一个里面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。在Android 源代码中四处挖掘,我在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 覆盖。有人知道解决这个问题的方法吗?