2014-03-05 40 views
0

我想创建一个可重用的表格布局,其中我有3行,最后一行仅包含1个元素,而前两个包含2个元素。表格布局中的等高行

现在,问题是,尽管对每一行赋予相同的权重,但表格布局中的行并不具有相同的高度。 底排消耗太多空间。

以下是布局内容:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="3" 
    > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="2" > 

     <TextView 
      android:id="@+id/txtvw_age" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/age" > 
     </TextView> 

     <TextView 
      android:id="@+id/txtvw_roll_no" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/roll_no" > 
     </TextView> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="2" > 

     <TextView 
      android:id="@+id/txtvw_standard" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/standard" > 
     </TextView> 

     <TextView 
      android:id="@+id/txtvw_section" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/section" > 
     </TextView> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     > 

     <TextView 
      android:id="@+id/txtvw_ct_name" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="@string/ct_name" > 
     </TextView> 
    </TableRow> 
</TableLayout> 
+0

“TableRow”的任何高度和宽度设置都被忽略,并且'TableLayout'自动将'fill_parent'设置为'layout_width'和'wrap_content'设置为'layout_height' –

回答

1

试试这个:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="3" 
    > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="2" > 

     <TextView 
      android:id="@+id/txtvw_age" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/save" > 
     </TextView> 

     <TextView 
      android:id="@+id/txtvw_roll_no" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/save" > 
     </TextView> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:weightSum="2" > 

     <TextView 
      android:id="@+id/txtvw_standard" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/save" > 
     </TextView> 

     <TextView 
      android:id="@+id/txtvw_section" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/save" > 
     </TextView> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/txtvw_ct_name" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="@string/save" > 
     </TextView> 
    </TableRow> 
</TableLayout> 
+0

您的更改似乎工作正常,但我我无法理解这些改变是如何运作的。如果你添加了一些说明,这将是很好的。但是,我得到了一个更多的解决方案,并且也将其作为答案。 – guptakvgaurav

0

我提出了以下变化到最后TableRow和一切顺利

<TableRow 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:weightSum="1" 
    > 

    <TextView 
     android:id="@+id/txtvw_ct_name" 
     android:layout_width="0dp"  // this t.v will consume all width 
     android:layout_weight="1" 
     android:layout_height="match_parent" // consume all height as of parent which is adjusted by weight. 
     android:text="@string/save" 
     > 
    </TextView> 
</TableRow> 

因为table布局有重量总和3,我提供与表行(wrt高度)相等的重量。