【发布时间】:2018-12-23 21:12:29
【问题描述】:
我在 SO 上读到,为了获得每列具有相同宽度的表格布局,您可以将 android:width="0dp" 和 android:weight="1" 设置为布局参数并且它可以工作。
我会得到相同的结果,但以编程方式,所以我尝试了这段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_score, container, false);
TableRow row = (TableRow)rootView.findViewById(R.id.playerRow);
for (Player p : game.getPlayers()) {
TextView t = new TextView(rootView.getContext());
t.setText(p.getName());
row.addView(t, new TableLayout.LayoutParams(0, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
// with the previuos line of code, nothing is showed
// instead this work: row.addView(t) , but each column doesn't take the maximum width (as I would)
}
return rootView;
}
但正如评论中所解释的,它没有按预期工作。 我无法得到我缺少的东西。
【问题讨论】:
-
试试这个:
row.addView(t, new TableLayout.LayoutParams(parent.getWidth()/numColumns, TableLayout.LayoutParams.WRAP_CONTENT, 0));where numColumns == 表中的列数 -
同样的结果。我试过
row.addView(t, new TableLayout.LayoutParams(row.getWidth()/game.getPlayers().size(), TableLayout.LayoutParams.WRAP_CONTENT, 1f));,没有任何改变