2014-03-05 82 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高度)相等的重量。