Android:表格单元格的高度应该是前一个单元格的高度

时间:2016-06-04 17:14:40

标签: android height android-tablelayout tablerow

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

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

这很有效。 问题是删除按钮会阻止表格行增加高度,因此它部分隐藏,如下所示:

hidden row

这是生成一个表行的代码的重要部分:

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或任何其他好的解决方案(但我不会计算成分字符或其他东西。我想这可以通过一些表属性来解决或类似的)。任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

因此,您只需在声明TableRow时添加一条语句即可达到您的要求。

          tableRow.setGravity(Gravity.FILL);