2014-04-22 86 views
1

我試圖用3個TableRows繪製一個TableLayout。該表是在一個xml文件中定義的(見下文),它擴大了DialogFragment。表的所​​有3行都有DialogFragment中的TableLayout不尊重「wrap_content」屬性

layout_height = "wrap_content" 

並且每行的高度計算正確。但是,封閉的TableLayout包含了一些大的餘量,並沒有像應該那樣「包裝」它的內容。我在這裏做錯了什麼?

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

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/borderless_blue_5dp" > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:padding="5dp" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:text="@string/CHANGE_ROOM" 
       android:textSize="20sp" /> 
     </TableRow> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/web_safe_silver" /> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/border" 
      android:padding="5dp" > 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:text="@string/CLIENT" 
       android:textSize="@dimen/text_size_regular" /> 

      <TextView 
       android:id="@+id/tv_client_name" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="4" 
       android:background="@drawable/dark_rect_smooth" 
       android:textColor="@color/discoveroom_gray" 
       android:textSize="@dimen/text_size_regular" /> 
     </TableRow> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/web_safe_silver" /> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/border" 
      android:weightSum="10" 
      android:padding="5dp" > 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:text="@string/ROOM" 
       android:textSize="16sp" /> 

      <TextView 
       android:id="@+id/room_number" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:background="@drawable/dark_rect_smooth" 
       android:textColor="@color/discoveroom_gray" 
       android:textSize="@dimen/text_size_regular" /> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:text="@string/CHARGE" 
       android:textSize="@dimen/text_size_regular" /> 

      <TextView 
       android:id="@+id/charge" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="3" 
       android:background="@drawable/dark_rect_smooth" 
       android:textColor="@color/discoveroom_gray" 
       android:textSize="@dimen/text_size_regular"/> 
     </TableRow> 
    </TableLayout> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@color/web_safe_silver" /> 

    <ListView 
     android:id="@+id/alternative_rooms_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:choiceMode="singleChoice" 
     android:visibility="invisible" > 
    </ListView> 

    <LinearLayout 
     android:id="@+id/edit_buttons" 
     style="?android:attr/buttonBarButtonStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:visibility="invisible" > 

     <Button 
      android:id="@+id/btn_close" 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:text="@string/CANCEL" 
      android:textColor="#894556" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn_ok" 
      style="?android:attr/buttonBarButtonStyle" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:text="@string/OK" 
      android:textColor="#489456" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

</LinearLayout> 

TableLayout in DialogFragment contains big margin around TableRows

回答

1

它工作時,我更換了最外層的LinearLayout與RelativeLayout的。因此,我知道如何解決這個問題,但我仍然不明白爲什麼會發生這種情況。歡迎發表其他意見。

+0

我也有同樣的問題。所以我用Relative替換了根佈局。現在它正在工作。感謝您發佈解決方案。 –