12

我遇到了由包含LinearLayout的ScrollView組成的片段的問題。我試圖創建一個效果,其中LinearLayout具有白色背景,看起來像是在彩色背景上滾動的一張紙。我試圖達到這個目標的方式是讓ScrollView佔據片段的全部空間,然後LinearLayout裏面有android:layout_margin="16dp"來創建「紙張」周圍的空間。ScrollView不滾動到內部LinearLayout的底部邊距的末尾

這樣,ScrollView的滾動條會出現在彩色背景區域中,頂部的邊距會隨着內容一起滾動並且底部的邊距只會在到達結尾時滾動。

不幸的是,在這個配置中,ScrollView不會一直滾動到最後,實際上會在底部切掉很少量的文本。我懷疑ScrollView在垂直滾動距離中沒有考慮到它的孩子的邊距。爲了解決這個問題,我將LinearLayout包裝在一個FrameLayout中,它解決了這個問題,但似乎是多餘的。任何指針如何消除這個不需要的容器將不勝感激。

注意:在滾動視圖上設置android:padding="16dp"並刪除邊距不會產生所需的效果,因爲無論滾動位置如何,連續出現在所有四個邊上的填充。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    tools:context=".ArticleFragment" > 

    <!-- This FrameLayout exists purely to force the outer ScrollView to respect 
     the margins of the LinearLayout --> 
    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="10dp" 
      android:layout_margin="16dp" 
      android:background="@color/page_background" > 

      <TextView 
       android:id="@+id/article_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textIsSelectable="true" /> 

      <TextView 
       android:id="@+id/article_content" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textIsSelectable="true" /> 

     </LinearLayout> 
    </FrameLayout> 
</ScrollView> 
+1

嘗試設置'機器人:layout_height =「FILL_PARENT」'的'ScrollView'? – 2013-05-08 14:50:46

+0

@AdilSoomro是的,沒有喜悅。 ScrollView的大小並不是一個真正的問題 - 它會適當地填充可用的空間。這是它提供的滾動區域 - 當它的直接孩子有餘量時,它不允許滾動到其內容的最後。我相信這個問題是由於它在計算滾動區域時沒有考慮到孩子的邊際。 – 2013-05-09 10:06:27

回答

0

我擺脫FrameLayout的,使LinearLayoutScrollView的孩子match_parent集作爲佈局的高度。如果仍然不起作用,請考慮用另一個任意View替換底部邊距,並將所需高度和背景作爲LinearLayout中的最後一項。

+0

Match_parent必須位於scrollView中。並且scrollView的子元素必須有高度=「wrap_content」。 – 2013-05-08 15:10:00

+0

啊好點。後一種選擇可能是最好的。 – Wenger 2013-05-08 16:40:04

+2

這些組合都不能解決問題。說實話,在LinearLayout的末尾添加一個額外的期望邊距高度的視圖要比將其包裝在FrameLayout中要粗糙一些。爲什麼ScrollView以這種方式運行並忽略頂部和底部邊距? – 2013-05-09 09:19:44

5

我記得ScrollView在內容佈局有一個上邊距時不知何故沒有達到它的內容的末尾。我解決了這個問題用少許劈,添加一個空的視圖到LinearLayout的末尾:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".ArticleFragment" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dp" 
     android:layout_margin="16dp" > 

     <TextView 
      android:id="@+id/article_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textIsSelectable="true" 
      android:background="@color/page_background" /> 

     <TextView 
      android:id="@+id/article_content" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textIsSelectable="true" 
      android:background="@color/page_background" /> 


     <!-- Add a little space to the end --> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="30dp" /> 

    </LinearLayout> 
</ScrollView> 

我使用了類似的空視圖也在LinearLayout的開頭,以避免頂部/底部邊緣完全。

編輯:我只是意識到,你也希望「論文」的結束時顯示出來,當達到視圖的結尾。在這種情況下,您可能需要將背景顏色設置爲TextView而不是佈局本身。然後確保標題和文章之間沒有空白(使用填充作爲分隔)。

編輯2:我應該學會檢查問題的提出時間......好吧,也許這仍然有助於某人。 :)

1

問題是舊的,但我已經有問題,當包裝FrameLayout ScrollView時行爲不端。它似乎也沒有考慮包含佈局的邊際。您可以將FrameLayout替換爲另一個應該正確測量的獨子LinearLayout。我已經完全去除的FrameLayout和簡單地添加填充總結保證金在內的佈局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="26dp" 
      android:background="@color/page_background" > 
<!--...-->