是否可以指定TableRow的高度? [英] Is it possible to specify TableRow height?

查看:330
本文介绍了是否可以指定TableRow的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableLayout,里面有多个TableRow视图.我希望以编程方式指定行的高度.例如

I have a TableLayout with multiple TableRow views inside it. I wish to specify the height of the row programatically. E.g.

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()方法触发):

But this isn't working and is defaulting to WRAP_CONTENT. Digging around in the Android source code, I see this in TableLayout (triggered by the onMeasure() method):

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覆盖.有人知道解决这个问题的方法吗?

Seems like any attempt to set the row height will be overridden by the TableLayout. Anyone know a way around this?

推荐答案

好的,我想我现在已经掌握了这个窍门.设置行高的方法不是摆弄附加到TableRowTableLayout.LayoutParams,而是摆弄附加到 any 单元格的TableRow.LayoutParams.只需将一个单元格设置为所需的高度,然后(假定其最高的单元格)整个行将为该高度.就我而言,我将额外的1像素宽的列设置为所需的高度,从而达到了目的:

OK, I think I've got the hang of this now. The way to set the height of the row is not to fiddle with the TableLayout.LayoutParams attached to the TableRow, but the TableRow.LayoutParams attached to any of the cells. Simply make one cell the desired height and (assuming its the tallest cell) the entire row will be that height. In my case, I added an extra 1 pixel wide column set to the desired height which did the trick:

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));

这篇关于是否可以指定TableRow的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆