我有一個TableLayout
其中有多個TableRow
視圖。我希望以編程方式指定行的高度。例如。是否可以指定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 source code周圍挖,我看到這個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覆蓋。任何人都知道解決這個問題?
謝謝,但上面的引用代碼的最後一行(見''findLargestCells()'')無論我指定什麼,TableLayout源代碼都將ViewGroup.LayoutParams的高度重置爲WRAP_CONTENT。 –