我正在尝试在Android中创建一个表(最低API级别10),其中某些列必须尽可能地延伸,而另一些则需要缩小以适应其内容的大小。
这就是我所拥有的:
(颜色只是看单元格的宽度和高度)
我在说“在”列。我需要它看起来像这样一个:
http://img268.imageshack.us/img268/1572/77141b8b8eb443d783cede5.png
这是我现在使用的代码:
ScrollView sv = new ScrollView(this);
TableLayout table = new TableLayout(this);
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
table.setPadding(10, 10, 10, 10);
//title row
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TextView title = new TextView(this);
title.setText("Majors - Season 10");
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 4;
rowTitle.addView(title, params);
//header row
TableRow rowHeaders = new TableRow(this);
TextView away = new TextView(this);
away.setText("Away");
away.setTypeface(Typeface.DEFAULT_BOLD);
away.setGravity(Gravity.CENTER_HORIZONTAL);
rowHeaders.addView(away);
TextView empty = new TextView(this);
empty.setText(" ");
// empty.setGravity(Gravity.CENTER_HORIZONTAL);
empty.setBackgroundColor(Color.GREEN);
rowHeaders.addView(empty);
TextView home = new TextView(this);
home.setText("Home");
home.setTypeface(Typeface.DEFAULT_BOLD);
home.setGravity(Gravity.CENTER_HORIZONTAL);
rowHeaders.addView(home);
TextView score = new TextView(this);
score.setText("Score");
score.setTypeface(Typeface.DEFAULT_BOLD);
score.setGravity(Gravity.RIGHT);
rowHeaders.addView(score);
TableRow rowMatch = createMatchRow();
TableRow rowMatch2 = createMatchRow();
table.addView(rowTitle);
table.addView(rowHeaders);
table.addView(rowMatch);
table.addView(rowMatch2);
sv.addView(table);
return sv;
createMatchRow()只是为每个匹配项返回TableRow的方法。 (第2行)
最佳答案
嗯,通过移除
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
并添加:
table.setColumnStretchable(0, true);
table.setColumnStretchable(2, true);