【Android】画面をスクロールさせる方法

ScrollViewを使用することで、スクロールバーを実現することができます。

スクロールバーの作成


レイアウトファイル(.xml)に以下を追加します。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
             />

        <EditText
             />

        <Button
             />

        .
        .
        .

    </LinearLayout>
</ScrollView>

ここで注意が必要なのは、ScrollViewのなかに、子要素を一つ入れる必要があるということです。

LinearLayoutがここでいう子要素になります。

子要素がないとScrollView can host only one direct childというエラーがでるので注意して下さい。


参考サイト:

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

ScrollView can host only one direct child -でじうぃき