2011-07-11 117 views
52

可能重複:
XML Table layout? Two EQUAL-width rows filled with equally width buttons?Android中設置表佈局列的寬度相等

我使用TableLayout以顯示4列數據的列表。

問題描述:

我無法將所有4列,這是我TableLayout的寬度相等。 我把我的佈局代碼,我使用...

<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0"> 
     <TableRow> 
      <TextView android:text="table header1" android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textSize="10dip" android:textStyle="bold"/>  
      <TextView android:text="table header2" android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textSize="10dip" android:textStyle="bold"/> 
      <TextView android:text="table header3" android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textSize="10dip" android:textStyle="bold"/> 
      <TextView android:text="table header4" android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textSize="10dip" android:textStyle="bold"/>   
     </TableRow> 
    </TableLayout> 

我應該如何改寫這個佈局,以顯示與大小相等4列?

+1

http://stackoverflow.com/q/2865497/778427 –

+0

@Glendon Trullinger waoooo其工作。謝謝。 –

回答

143

Try this.

它歸結爲增加android:stretchColumns="*"TableLayout根,並在您TableRow S設定android:layout_width="0dp"所有的孩子們。

<TableLayout 
    android:stretchColumns="*" // Optionally use numbered list "0,1,2,3,..." 
> 
    <TableRow 
     android:layout_width="0dp" 
    > 
+15

請注意......我還必須爲'TableRows'的子元素添加'android:layout_weight =「1」'使其工作。 – Asaf

+28

stretchColumns應該是「*」,而不是1使所有列的寬度相等。 – Yevgeniy

+3

這個解決方案適用於我,但我一直在尋找以編程的方式。我在每個'TableRow'子節點上對'TableLayout'和'setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT))'使用'setStretchAllColumns(true)'。設定體重似乎沒有必要。 –

63

變化android:stretchColumns值*

值0表示拉伸塔1值1意味着拉伸塔2等。

值*手段舒展的所有列

+8

它不工作,如果column1是文本更多,它的擴展。 – Palani

+1

是的,這是行不通的 – Ted

+11

只需刪除你的android:stretchColumns =「0」,並將android:layout_weight =「1」添加到所有四個,你會得到所需的輸出。 – user2779311

相關問題