[Android] ScrollView

강승구·2022년 5월 15일
0

ScrollView는 구현하려는 내용의 높이가 실제 화면의 높이보다 클 때 화면을 스크롤할 수 있도록 하기 위해 사용한다.
ScrollView를 이용하여 내용을 감싸주면 화면의 높이보다 내용물이 커질 때 자동으로 스크롤이 가능하도록 된다.

ScrollView는 하나의 view만 가질 수 있기 떄문에 여러개의 ScrollView 내부에 여러 view들을 그리기 위해서는 내부에 LinearLayout같은 layout을 두어 그려야한다.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/cancle_red" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/yello" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/sky_sumin" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/orange_june" />
    </LinearLayout>
</ScrollView>

수평으로 스크롤을 하기 위해서는 HorizontalScrollView를 이용할 수 있다.

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView 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="horizontal">

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:text="1"
            android:background="@color/cancle_red" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@color/yello" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@color/sky_sumin" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@color/orange_june" />
    </LinearLayout>
</HorizontalScrollView>
profile
강승구

0개의 댓글