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

查看:34
本文介绍了是否可以指定 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?

推荐答案

好的,我想我现在掌握了这个窍门.设置行高的方法不是摆弄附在TableRow上的TableLayout.LayoutParams,而是附上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天全站免登陆