Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:表格单元格的高度应该得到上一个单元格的高度_Android_Height_Android Tablelayout_Tablerow - Fatal编程技术网

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

Android:表格单元格的高度应该得到上一个单元格的高度,android,height,android-tablelayout,tablerow,Android,Height,Android Tablelayout,Tablerow,我正在以编程方式创建一个TableLayout。表格行可以由数量+单位(单元格1)、成分(单元格2)和删除按钮(单元格3)组成。 配料可以比可用宽度长,因此我使用了“权重”属性并将其设置为1以启用换行: setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f)); 这很有效。 问题是“删除”按钮阻止表格行增加高度,因此部分隐藏,如下所示: 这是生成一个表行的代码的重要部分: fin

我正在以编程方式创建一个
TableLayout
。表格行可以由数量+单位(单元格1)、成分(单元格2)和删除按钮(单元格3)组成。 配料可以比可用宽度长,因此我使用了“权重”属性并将其设置为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等。因此,setGravity()的任何值(而不是TOP)都将以完整内容呈现TextView。有关更多详细信息,请咨询官方

因此,您只需在声明TableRow时添加一条语句即可实现您的需求

          tableRow.setGravity(Gravity.FILL);