我遇到了由包含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>
嘗試設置'機器人:layout_height =「FILL_PARENT」'的'ScrollView'? – 2013-05-08 14:50:46
@AdilSoomro是的,沒有喜悅。 ScrollView的大小並不是一個真正的問題 - 它會適當地填充可用的空間。這是它提供的滾動區域 - 當它的直接孩子有餘量時,它不允許滾動到其內容的最後。我相信這個問題是由於它在計算滾動區域時沒有考慮到孩子的邊際。 – 2013-05-09 10:06:27