我正在以TableLayout
编程方式创建。表格行可以由数量+单位(单元格1),成分(单元格2)和删除按钮(单元格3)组成。成分可以长于可用宽度,因此我使用了weight属性并将其设置为1以启用换行符:
setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
这行得通。问题是删除按钮阻止表格行增加高度,因此它被部分隐藏,如下所示:
这是产生一个表行的代码的重要部分:
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
为该情况或任何其他好的解决方案(但我不会计算配料字符或其他内容。我想这可以通过某些表属性或类似方法解决)。任何想法如何解决这个问题?
TableRow是LinearLayout的直接子类。为了设置子视图在其中的放置方式,您需要定义其重力。默认值为TOP,我已经尝试过FILL,CENTER_VERTICAL等。因此,除TOP之外的setGravity()的任何值都将呈现具有完整内容的TextView。请参阅官方文档,以获取更多详细信息。
因此,您只需要在声明TableRow时添加一个语句即可达到您的要求。
tableRow.setGravity(Gravity.FILL);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句