2013-06-27 54 views
1

我想格式化我的tablelayout,但我似乎并没有得到很好的工作。 我有4列,我需要他们间隔尽可能好。我在想真的有3列宽度相同,最后一列占据空间的其余部分。最后一列可以换行两行或多行,但前三列必须是单行而不是截断。这里是我的代码表布局列间距

<TableLayout android:id="@+id/tlMylayout" android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:stretchColumns="*" android:shrinkColumns="*" > 
     <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> 

      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/showtext_style" android:text="col1" />    
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/showtext_style" android:text="col2" /> 
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/showtext_style" android:text="col3" />    
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/showtext_style" android:text="lastCol" /> 
     </TableRow> 
     <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> 

      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="2011-01-01 14:59" />   
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="50" /> 
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="0.57" />   
      <TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is the last one and can be very long string if I want it to be" /> 

     </TableRow> 


    </TableLayout> 

回答

3

尝试

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tlMylayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:shrinkColumns="*" 
android:stretchColumns="*" > 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="col1" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="col2" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="col3" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="lastCol" /> 
</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="2011-01-01 14:59" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:singleLine="true" 
     android:text="50" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:singleLine="true" 
     android:text="0.57" /> 

    <TextView 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight=".25" 
     android:gravity="center" 
     android:text="this is the last one and can be very long string if I want it to be" /> 
</TableRow> 

我删除layout_gravity并添加layout_weightTextView

+0

真棒。有效!谢谢 – Snake