我按照https://stackoverflow.com/a/7123217/2884981的示例进行了操作,但不幸的是,该行没有延伸到表格的整个宽度(请参阅右边的黑色“边框”/背景如此大,而它的宽度仅为1px其他人)。
我希望第二列扩展,因此它占用了右边的所有可用空间。
这是我正在使用的代码。拜托,如果有人知道我怎么能把这个正确的“边界”变成1px,而不是看起来那么糟糕,请告诉我。
for (int i = 0; i < tasks.size(); i++)
{
TaskDetail taskDetail = tasks.get(i);
TableRow row = new TableRow(this);
// Set the background/border colour to black
row.setBackgroundColor(Color.BLACK);
row.setPadding(1, 1, 1, 1);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0,0,1,0);
LinearLayout detailCell = new LinearLayout(this);
detailCell.setBackgroundColor(Color.parseColor("#FFE6F0"));
detailCell.setLayoutParams(layoutParams);
LinearLayout valueCell = new LinearLayout(this);
valueCell.setBackgroundColor(Color.WHITE);
valueCell.setLayoutParams(layoutParams);
TextView detailName = new TextView(this);
detailName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
detailName.setText(taskDetail.getName());
detailName.setPadding(5, 10, 5, 5);
detailName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView valueName = new TextView(this);
valueName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
valueName.setText(taskDetail.getValue());
valueName.setPadding(5, 10, 5, 5);
valueName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
detailCell.addView(detailName);
valueCell.addView(valueName);
// Add to the screen layout
row.addView(detailCell);
row.addView(valueCell);
table.addView(row);
}
答案 0 :(得分:1)
为行中的每一列设置weight
为1.0f
,如下所示:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
只需按以下方式进行更改:
...
...
row.setPadding(1, 1, 1, 1);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
layoutParams.setMargins(0,0,1,0);
LinearLayout detailCell = new LinearLayout(this);
...
...