2011-07-11 114 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

相关问题