【发布时间】:2012-03-29 19:02:18
【问题描述】:
我正在尝试动态创建一个 TableLayout,它的所有行都被拉伸,如 in this tutorial 所示。我已经通过 XML 完成了它,但我想从 Activity 中完成它。这是我迄今为止尝试过的代码,但没有成功:
public View getTableWithAllRowsStretchedView() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(4);
for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
for (int j = 0; j < 4; j++) {
Button button = new Button(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
linearLayout.addView(tableLayout);
return linearLayout;
}
非常感谢您。
编辑
Screenshot of what I have
what I expect
【问题讨论】:
-
您可以添加当前和预期的屏幕截图吗?