我正在尝试动态设置活动中的表布局。行数和列数取决于用户的输入。
所以问题 -每次行数都很好,但每行中的列数(textViews)总是等于数组no_periods[ ]
的最低值。如。如果no_days的值为5,则no_periods[]
的值为2,3,4,5,6。表将有5行,但每一行将只有2个textview,而不是在各自的行中有2,3,4,5,6个textview。
这是我正在尝试的代码
//loop to set the rows and text view in the table layout of activity_home
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(TimeTable.this);
for (int j = 0; j <no_periods[i]; j++) {
textView = new TextView(TimeTable.this);
textView.setHint("Click");
textView.setClickable(true);
textView.setOnClickListener(click_listen);
int maxpixels = screenWidth/(no_periods[i]);
int minpixels = maxpixels;
textView.setPadding(10, 10, 10, 10);
textView.setMaxWidth(maxpixels);
textView.setMinWidth(minpixels);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
使用TableRow weight:
代替max和minWidthtextView.setPadding(10, 10, 10, 10);
tableRow.addView(textView, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
这将在每一行的textview之间分配水平空间。
p。您可能也想使用textView.setGravity(Gravity.CENTER);