我讀過SO,爲了獲得每個列具有相同寬度的表格佈局,您可以將android:width="0dp"
和android:weight="1"
設置爲佈局參數,並且它可以工作。 我會得到相同的結果,但是編程,所以我想這大塊的代碼:TableLayout中的相同寬度的列,*編程*
@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));'其中numColumns ==表中的列數 – zgc7009
相同的結果。我嘗試'row.addView(t,new TableLayout.LayoutParams(row.getWidth()/ game.getPlayers()。size(),TableLayout.LayoutParams.WRAP_CONTENT,1f));',沒有任何更改 –