2010-11-18 69 views
1

我有一個RelativeLayout,它包含三個元素:頂部的TableLayout包含一些控件(layout_alignParentTop = true),底部的LinearLayout(layout_alignParentBottom = true),包含一對按鈕和在它們之間延伸的ListView(使用layout_below和layout_above這兩個元素)。這很好,但在某些情況下,LinearLayout的底部需要消失。如果我使用setVisibility將其設置爲View.GONE或使用setLayoutParams()將其高度設置爲0,則ListView將頑固地拒絕調整大小以佔用新釋放的空間。我已經嘗試調用requestLayout()基本上在層次結構中的所有內容,似乎沒有任何工作。如果我啓動層次結構查看器,則可以看到該視圖已經消失/具有0的layout_height,但它仍然不起作用。具有諷刺意味的是,在層次結構查看器完成之後,佈局會自行修正。不過,我認爲讓我的用戶運行層次結構查看器可能是不可接受的。Android:設置視圖高度爲0

編輯:中間佈局實際上是一個包含ListView的RelativeLayout,這可能很重要,我會嘗試並消除它。我已經發布了我的XML的簡化版本,可以提供我正在查看的內容。

<RelativeLayout 
    android:id="@+id/main_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/top_table" 
     android:layout_alignParentTop="true" 
     android:stretchColumns="2" 
     android:shrinkColumns="1"> 
     <!-- removed for simplicity --> 
    </TableLayout> 
    <RelativeLayout 
     android:id="@+id/main_content" 
     android:descendantFocusability="afterDescendants" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/title_table" 
     android:layout_above="@id/ad_toolbar"> 
     <!-- removed for simplicity --> 
     <ListView 
      android:id="@+id/android:list" 
      android:layout_alignParentTop="true" 
      android:descendantFocusability="afterDescendants" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/bottom_layout" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
     <!-- removed for simplicity --> 
    </LinearLayout> 
</RelativeLayout> 
+0

你可以發佈.xml的佈局嗎? – 2010-11-18 01:37:16

+0

實際的XML非常複雜,因爲佈局由幾個由不同活動使用的佈局文件組成,但是我已經發布了上面的簡化版本。我意識到中間區域實際上是一個包含ListView的RelativeLayout,它可能會有所作爲,我將嘗試並消除它。 – jjb 2010-11-18 17:51:28

回答

1

是否有一個特定的原因,你使用RelativeLayout爲父?這聽起來像是應該使用包含TableLayout,ListView和LinearLayout(按此順序)的垂直LinearLayout,其中ListView具有layout_height =「0dip」和layout_weight =「1」。當您在底部LinearLayout上設置可見性時,ListView的佈局權重應該使其展開以填充空間。

+0

layout_weight是正確的解決方案。或者他還沒有想到,或者這個問題比我們想象的更復雜?我雖然懷疑它。這就是爲什麼XML會很有用。比我們可以快速修復它:D – 2010-11-18 03:07:01

+0

它是一個RelativeLayout,因爲包含頂部和底部元素的佈局文件用於多個活動,其中一些使用了RelativeLayout來適當定位子元素。並非所有可能的佈局都可以像​​LinearLayout一樣輕鬆工作。 – jjb 2010-11-18 17:53:46

相關問題