Androidアプリで画面の一部をスクロールさせる場合は、ScrollView
のlayout_above
layout_below
を使用します。
ヘッダやフッタがある画面でメインの部分だけスクロールさせたい場合などに便利です。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
...
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/footer"
android:layout_below="@id/header" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
...
</LinearLayout>
</RelativeLayout>
ScrollView
とその周りの要素は並列の階層であるのがよさそうです。
少し試してみたところでは、以下のようにフッタと並列なレイアウトの中にScrollView
を書くとうまく動きませんでした。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/footer" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
...
</LinearLayout>
</RelativeLayout>
※ こちらの記事を参考にしました。