2014-05-15 27 views
0

我讀過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; 
} 

但在評論的解釋,預期這是行不通的。 我無法得到我想念的東西。

+0

試試這個:'row.addView(t,new TableLayout.LayoutParams(parent.getWidth()/ numColumns,TableLayout.LayoutParams.WRAP_CONTENT,0));'其中numColumns ==表中的列數 – zgc7009

+0

相同的結果。我嘗試'row.addView(t,new TableLayout.LayoutParams(row.getWidth()/ game.getPlayers()。size(),TableLayout.LayoutParams.WRAP_CONTENT,1f));',沒有任何更改 –

回答

6

有一個在我的代碼中的錯誤,是的LayoutParams錯課,我應該用TableRow.LayoutParams代替TableLayout.LayoutParams,所以:

row.addView(t, new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f)); 

按預期工作。

0

你可以嘗試這樣。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <!-- 2 columns --> 
    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/textView1" 
      android:text="Column 1" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <Button 
      android:id="@+id/button1" 
      android:text="Column 2" /> 
    </TableRow> 

    <!-- edittext span 2 column --> 
    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_span="2" 
      android:text="Column 1 &amp; 2" /> 
    </TableRow> 

    <!-- just draw a red line --> 
    <View 
     android:layout_height="2dip" 
     android:background="#FF0000" /> 

    <!-- 3 columns --> 
    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <TextView 
      android:id="@+id/textView2" 
      android:text="Column 1" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <Button 
      android:id="@+id/button2" 
      android:text="Column 2" /> 

     <Button 
      android:id="@+id/button3" 
      android:text="Column 3" /> 
    </TableRow> 

    <!-- display this button in 3rd column via layout_column(zero based) --> 
    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <Button 
      android:id="@+id/button4" 
      android:layout_column="2" 
      android:text="Column 3" /> 
    </TableRow> 

    <!-- display this button in 2nd column via layout_column(zero based) --> 
    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <Button 
      android:id="@+id/button5" 
      android:layout_column="1" 
      android:text="Column 2" /> 
    </TableRow> 

</TableLayout> 
+0

我在尋找一種方式編程 –

+0

好的,請等待 – kablu

+0

你可以按照1. http://puneetaggarwal.in/android/android-tablelayout-programatically/或2. http://stackoverflow.com/questions/7226168/android- tablelayout-以編程方式 – kablu