Android:table 单元格的高度应为前一个单元格的高度

Android: Height of table cell should get height of previous cell

我正在以编程方式创建 TableLayout。 table 行可以包含数量 + 单位(单元格 1)、成分(单元格 2)和删除按钮(单元格 3)。 成分可以比可用宽度长,所以我使用了 weight 属性并将其设置为 1 以启用换行符:

setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));

这行得通。 问题是删除按钮阻止 table 行增加高度,因此它被部分隐藏,如下所示:

这是生成 table 行代码的重要部分:

final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setTag(INGREDIENT_ENTRY);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

// Amount and unit
int dp6InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 6);
TextView tvAmountAndUnitText = new TextView(getApplicationContext());
tvAmountAndUnitText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvAmountAndUnitText.setText(strAmount + " " + strUnit);
tvAmountAndUnitText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvAmountAndUnitText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tvAmountAndUnitText.setGravity(Gravity.RIGHT);
tvAmountAndUnitText.setTypeface(tvAmountAndUnitText.getTypeface(), Typeface.BOLD);
tvAmountAndUnitText.setPadding(dp6InPixel, 0, dp6InPixel, 0);
tableRow.addView(tvAmountAndUnitText);

// Ingredient
TextView tvIngredientText = new TextView(getApplicationContext());
tvIngredientText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
tvIngredientText.setText(strIngredient);
tvIngredientText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tableRow.addView(tvIngredientText);

// Button
int dp10InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 10);
TextView tvIngredientDeleteButton = new TextView(getApplicationContext());
LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(dp10InPixel, 0, 0, 0);
tvIngredientDeleteButton.setLayoutParams(buttonParams);
tvIngredientDeleteButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientDeleteButton.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.lightred));
tvIngredientDeleteButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
tvIngredientDeleteButton.setPadding(dp6InPixel, dp6InPixel, dp6InPixel, dp6InPixel);
tvIngredientDeleteButton.setText("x");
//more code

tableRow.addView(tvIngredientDeleteButton);
ingredientTable.addView(tableRow); 

当我设置 tvIngredientDeleteButton.setMinLines(2); 时,我可以看到完整的成分单元格。不幸的是,所有行的最小高度都为 2,这看起来很难看。我需要一些方法来识别成分单元格是否有换行符并为这种情况设置 minLines 或任何其他好的解决方案(但我不会计算成分字符或其他东西。我想这可以通过一些 table 属性或类似)。任何想法如何解决这个问题?

TableRow 是 LinearLayout 的直接子类。为了设置子视图在其中的定位方式,您需要定义它的重力。默认值为 TOP,我已经尝试使用 FILL、CENTER_VERTICAL 等。因此除 TOP 之外的任何 setGravity() 值都将呈现具有完整内容的 TextView。详情请参考官方document

所以只需要在声明TableRow时一次添加一条语句就可以达到你的要求。

          tableRow.setGravity(Gravity.FILL);