LoginSignup
34
28

More than 5 years have passed since last update.

Androidアプリで画面の一部をスクロールさせる

Posted at

Androidアプリで画面の一部をスクロールさせる場合は、ScrollViewlayout_abovelayout_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>

こちらの記事を参考にしました。

34
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
28