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

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

最佳答案

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

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

          tableRow.setGravity(Gravity.FILL);

关于Android:表格单元格的高度应该得到前一个单元格的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632964/

相关文章:

android - 如何在android中以全屏模式显示电池图标和Wifi图标?

android - 完成第一个 Activity 后开始第二个 Activity

fonts - CSS3 : Is there a way to alter Font Height?

c++ - 用于 Linux 的 C++ 中的屏幕分辨率

android - 从android中的表格布局中动态删除行

android - 在 Android 问题上远程调试 Chrome

java - 如何设置进度条以在android中加载数据的 Activity

javascript - Div 不会调整大小

android - 以编程方式设置 Imageview 的高度和宽度

java - 如何在 Android 中创建一个包含多列的表格?