2011-12-30 105 views
4

我想讓ScrollView在屏幕底部留出太多空間,因爲它會讓我的兩個按鈕不能顯示。我也不想爲ScrollView手動設置高度。保持ScrollView佔用太高的高度

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     ... 
     android:orientation="horizontal"> 
     <Button .../> 
     <TextView .../> 
     <Button .../> 
    </LinearLayout> 
    <ScrollView 
     android:layout_width="math_parent" 
     android:layout_height="wrap_content"> 
     <ImageView .../> 
     <ImageView .../> 
     <ImageView .../> 
    </ScrollView> 
    <LinearLayout 
     ... 
     android:orientation="horizontal"> 
     <Button .../> 
     <Button .../> 
    </LinearLayout> 
</LinearLayout> 

這是我的佈局的通用版本。正在發生的事情是ScrollView一直延伸到屏幕的底部,無論它有多少物品。這會導致屏幕底部的兩個按鈕不可見。

如何在不手動設置ScrollView高度的情況下阻止發生這種情況?

我用我所有的觀點使用android:layout_height="wrap_content"

是不是應該自動分配視圖的高度來適應屏幕的高度?

(希望包括圖像,但我的代表處是不夠高,但( - _ - ))

+1

這可能是切換到的RelativeLayout作爲根佈局的好主意。它應該爲您提供工具,讓ScrollView保持在視圖的中心區域內,而無需自己明確計算其高度。 – harism 2011-12-30 15:06:37

+0

感謝您的評論。我已經實施了一個不同的解決方案,但是我一路上學到了更多的東西,我相信你的實現將最適合我想做的事情。一個RelativeLayout將允許我將Buttons錨定到屏幕底部,然後將所有其他UI對象放置在屏幕頂部和按鈕之間。 – edc598 2012-02-01 03:51:28

+0

你的ScrollView應該只有一個孩子。不知道這是不是你的問題的原因。 – Wytze 2014-04-14 14:20:37

回答

2

的方式我今天解決這個問題的方法是使用RelativeLayout作爲基地。將兩個LinearLayout分別固定到RelLayout的頂部和底部。然後我會插入ScrollView,但我會確保設置它的佈局屬性如下:

android:layout_below="topLinearLayout" android:layout_above="bottomLinearLayout"

+0

不應該是「alignParentTop = true」(底部相同) – ataulm 2013-09-16 20:33:39

+0

是的頂部LinearLayout你會把'alignParentTop = true'和底部的LinearLayout你會'alignParentBottom = true',但對於ScrollView我的方式它是否如上所述,換句話說,使ScrollView錨點的頂部邊緣到頂部LinearLayout的底部邊緣,底部邊緣到底部LinearLayout的頂部邊緣。 – edc598 2013-09-16 21:02:21

1

您是否嘗試過使用layout_weightScrollView。 IIRC中,如果爲一個元素設置了非零權重(並且對應的維度設置爲0dp),那麼在爲同級視圖設置測量值(在此情況下爲LinearLayout)之後,它將填充該維度中的剩餘空間在ScrollView的頂部和底部。

道歉,沒有得到一個Android環境檢查,它已經有一段時間。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout> 
     ... 
     Something at the top 
     ... 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="match_parent" 
     **android:layout_height="0dp" 
     android:layout_weight="1"**> 
     <ImageView .../> 
     <ImageView .../> 
     <ImageView .../> 
    </ScrollView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
     ... 
     buttons 
     ... 
    </LinearLayout> 
</LinearLayout> 

Here's an answer與有關layout_weight屬性的詳細信息。