Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以指定表格行高度?_Android_Android Tablelayout_Row Height - Fatal编程技术网

Android 是否可以指定表格行高度?

Android 是否可以指定表格行高度?,android,android-tablelayout,row-height,Android,Android Tablelayout,Row Height,我有一个TableLayout,里面有多个TableRow视图。我希望以编程方式指定行的高度。例如 int rowHeight = calculateRowHeight(); TableLayout tableLayout = new TableLayout(activity); TableRow tableRow = buildTableRow(); TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(

我有一个
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);
但这不起作用,默认为包装内容。在中,我在
表格布局中看到了这一点(由onMeasure()方法触发):

private void findLargestCells(int-widthmasurespec){
最终整数计数=getChildCount();
for(int i=0;i

似乎任何设置行高的尝试都将被TableLayout覆盖。有人知道解决方法吗?

首先,您应该使用显示因子公式将其从dps转换为像素

  final float scale = getContext().getResources().getDisplayMetrics().density; 

  int trHeight = (int) (30 * scale + 0.5f);
  int trWidth = (int) (67 * scale + 0.5f); 
  ViewGroup.LayoutParams layoutpParams = new ViewGroup.LayoutParams(trWidth, trHeight);
  tableRow.setLayoutParams(layoutpParams);

好的,我想我现在已经掌握了诀窍。设置行高度的方法不是摆弄
TableLayout.LayoutParams
附加到
TableRow
,而是附加到任何单元格的
TableRow.LayoutParams
。只需将一个单元格设置为所需的高度,并(假设它是最高的单元格)整行就是这个高度。在我的例子中,我添加了一个额外的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));

谢谢,但是上面引用的代码的最后一行(请参见TableLayout源代码中的findLargestCells()
)会重置ViewGroup.LayoutParams的高度以包装内容,而不管我指定了什么。
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));