2010-03-10 92 views
52

所以我的佈局基本上是這樣的內部:添加視圖佈局底部的滾動型

<ScrollView> 
    <RelativeLayout> 
     <BunchOfViews/> 
     <ImageView android:layout_alignParentBottom="true"/> 
    </RelativeLayout> 
</ScrollView> 

我有ScrollView因此,所有的佈局始終是可見無論屏幕的高度。問題是,在一個非常高的屏幕上,我仍然希望我的ImageView位於底部。然而,ScrollView的孩子似乎沒有定義底部。 View放置在佈局的頂部。我怎樣才能以一種整潔的方式解決這個問題?

回答

115

我碰到了同樣的問題。我從來沒有找到一個令人滿意的解決方案,但這是我如何做到的。也許別人有更好的方法,我討厭添加不做任何事情的佈局。

我的破解是在scrollview的底部添加一個虛擬linearlayout,它具有fill_parent來佔用所有空間並強制滾動視圖填滿屏幕。然後將我想要的任何組件添加到該線性佈局。

這裏是我的佈局,這是否之一:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:fillViewport="true" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="15px" > 

     <!-- bunch of components here --> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@+id/spinner" 
      android:layout_marginTop="5px" 
      android:gravity="center_horizontal|bottom" 
      android:paddingTop="2px" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="20px" 
       android:paddingRight="20px" 
       android:text="Delete" /> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
+0

謝謝,我終於明白了。你已經讓我的應用變得如此美麗! :) – pgsandstrom 2010-03-11 08:11:58

+0

@dweebo我不明白。它不適合我。你可以更具體的代碼演示,並顯示結束? – 2012-05-23 01:25:30

+35

android:fillViewport是技巧的重要組成部分,很容易錯過。 – 2013-12-31 10:49:55

0

來源:http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314

這會幫助你:

<RelativeLayout 
     android:layout_marginTop="-45dip" 
     android:padding="0dip" 
     android:layout_alignParentBottom="true" 
     android:gravity="bottom|right" 
     android:background="@android:drawable/bottom_bar" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
     <Button 
      android:id="@+id/manual_setup" 
      android:text="@string/account_setup_basics_manual_setup_action" 
      android:minWidth="@dimen/button_minWidth" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_marginBottom="-4dip" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="false" 
      /> 
     <Button 
      android:id="@+id/next" 
      android:text="@string/next_action" 
      android:minWidth="@dimen/button_minWidth" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:drawableRight="@drawable/button_indicator_next" 
      android:layout_marginBottom="-4dip" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="false" 
      /> 
    </RelativeLayout> 
+0

我很抱歉,但我不認爲我是很清晰。我仍然希望imageview成爲scrollview的一部分,就像我當前的佈局一樣。它應該在滾動視圖的底部。如果scrollview比屏幕更短,那麼imageview應該仍然在屏幕的底部。 – pgsandstrom 2010-03-10 14:22:05

+0

另外我不太明白負像素數字的影響。我從未使用過這些。所以也許我只是不明白你想說什麼。如果是這樣,你能否詳細說明一下? :P謝謝。 – pgsandstrom 2010-03-10 14:24:04

+1

我不明白你爲什麼需要滾動視圖,如果你的內容不填滿屏幕。在視圖上使用'android:gravity =「bottom | right」'應該放在底部。負值可能是爲了切斷按鈕佈局中的過多填充。 – Pentium10 2010-03-10 14:40:09

16

似乎LinearLayout中是沒有必要的,所有這些都是重要的是fillViewPort。 你可以只使用

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottomParent="true"> 

現在你已經指定了RelativeLayout的是在屏幕的至少大小。

+6

這實際上是有效的。答案不夠清楚 - 爲'ScrollView'添加'fillViewPort =「true」'。 – 2012-12-03 08:00:42

57

我有同樣的問題,找到了這個網頁:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

基本上,你設置了滾動的android:fillViewport爲真,這將讓孩子視圖擴展到相同的高度滾動型本身,填補空間。然後您只需要將其中一個子控件'layout_height設置爲fill_parentlayout_weight1,導致該控件「彈出」以填充空白空間。

請注意,如果ScrollView的內容已經足夠高以填充ScrollView,則android:fillViewport不起作用,所以該設置僅在需要時啓用。

我最後的XML看起來與此類似:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 
      <!-- this expands to fill the empty space if needed --> 
     </LinearLayout> 

     <!-- this sits at the bottom of the ScrollView, 
     getting pushed out of view if the ScrollView's 
     content is tall enough --> 
     <ImageView 
      android:layout_height="wrap_content" 
      android:src="@drawable/footer_image"> 
     </ImageView> 
    </LinearLayout> 
</ScrollView> 
+1

這很棒,但是填充屏幕的項目是一個EditText,並且在輸入大量行的情況下,允許它在屏幕底部以下增長。有沒有辦法來防止這種情況發生,只有在軟鍵盤顯示時才能顯示滾動視圖? – finiteloop 2011-05-15 16:50:00

+1

這很棒。簡潔整潔 – Maggie 2011-08-19 08:04:40

+0

謝謝! fillViewport = true很好地訣竅。 – 2014-07-02 17:02:00

0
ScrollView view = new ScrollView(this); 
ScrollView.LayoutParams lps = new FrameLayout.LayoutParams(FILL_PARENT, FILL_PARENT, Gravity.CENTER); 
LinearLayout layout = new LinearLayout(this); 
// what ever you want in the Layout 
view.addView(layout, lps); 
+0

你應該總是喜歡在XML中設置佈局高度和寬度,避免在代碼中進行。 – JaydeepW 2012-09-02 16:03:09

1

在你認爲你想成爲在底部採用Android:重力=「底」

4

喬爾·馬龍的回答Adding view to bottom of layout inside a scrollview這是否有竅門?我的解決方案几乎相同,除了我使用Space小部件來完成在父佈局內​​填充休息高度的工作。就像這樣:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent 
    android:fillViewport="true" 
    > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_height="wrap_content" 
     > 
     <android.support.v4.widget.Space 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      /> 
    <ImageView 
     android:layout_height="wrap_content" 
     android:src="@drawable/footer_image" 
     /> 
    </LinearLayout> 
</ScrollView> 

注意:爲廢棄了很久

  • 在其他的答案fill_parent注意;
  • 比較爲空LinearLayout填補父佈局的其餘部分,我想Space是更合適的(它的設計做到這一點的工作。)
  • 最後,不要忘了,在ScrollView開始重要的屬性: android:fillViewport="true"。他們一起製造這個伎倆。
+0

它爲我工作1 +。謝謝你節省我的時間 – Ninja 2017-07-17 13:03:19

+1

雖然最高評分的方法工作,這是我最喜歡的解決方案。使用空間是一個很好的技巧,它不需要額外的嵌套佈局,但仍然適用於多個「底部連接」元素。 – 2017-10-18 13:10:38

+0

一句話:由於'layout_weight =「1」'被設置,Space的高度應該被設置爲'「0dp」'。將高度設置爲'「match_parent」'將顯示相同的結果,但在Android Studio中顯示警告。 – 2017-10-19 09:03:43

5

其對我來說工作完美機器人:fillViewport =「真」

相關問題