2012-02-17 118 views
31

我想實現這一點:一個ScrollView包含許多元素(ImageViews,TextViews,EditTexts等),然後在ScrollView之後的一些按鈕(它們是自定義ImageViews)總是精確地出現在屏幕的底部。Android ScrollView和按鈕在屏幕的底部

如果我使用android:fillViewport =「true」屬性,那麼如果ScrollView的元素太大而不適合屏幕尺寸,則按鈕會變得不可見。如果我使用android:Weight = 1屬性,那麼當屏幕很大並且它可以適合(我希望按鈕佔用很小比例,大約10%)時,ScrollView只能獲得屏幕的50%。如果我將android:Weight設置爲更大的值,那麼按鈕顯得非常小。

請幫忙!也許這是簡單的,我忽略了,但我已經撞了我的頭幾個小時!因爲你把它放在一個線性佈局,因此線性佈局適合在屏幕

+0

請出示佈局文件 – 2012-02-17 10:12:29

回答

72

剛剛創建並對其進行測試。看起來像你想要的。

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <LinearLayout 
      android:id="@+id/buttons" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:gravity="center" 
      android:layout_alignParentBottom="true"> 
     <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Custom Button1"/> 
     <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Custom Button2"/> 
    </LinearLayout> 

    <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@id/buttons"> 
     <!--Scrollable content here--> 
     <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical">     
      <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="test text" 
        android:textSize="40dp"/> 
     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 
+0

這偉大工程!謝謝! – george 2012-02-17 10:49:13

+0

我有類似的問題。用這個解決方案很好地工作 – zolio 2012-10-15 01:34:54

+0

當ScrollView填滿時,它不會寫在按鈕上嗎? – FractalBob 2015-09-17 03:58:18

2

滾動視圖無法適應屏幕,

只能盡力使滾動視圖爲根的Elemen XML的佈局

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <!-- Here you can put some XML stuff and BOOM! your screen fit to scrollview --> 

    </LinearLayout> 
</ScrollView> 
9
<?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"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
      <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello World" 
      /> 
      <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hallo Welt" 
      />  
     </LinearLayout> 
    </ScrollView> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Go next page" 
      android:layout_alignParentRight="true" /> 
    </RelativeLayout> 

</LinearLayout> 
+0

嗨我有佈局與headder和滾動查看輸入字段和頁腳,當鍵盤到了頁腳也滾動up.how修復該頁腳在bottom.please幫助我 – Harsha 2015-11-18 05:34:54

+0

它的工作,我很難找到過去3小時的解決方案。 – chanakya 2017-01-11 02:47:02