우와테크

android 안드로이드 ScrollView 사용법 본문

java-androidStudio

android 안드로이드 ScrollView 사용법

uwaTech 2022. 5. 17. 09:36

화면가득 view를 만들고 ScrollView를 사용하지 않으면 스크롤이 되지않는다.

당연한 사실인데.. 인지하지 못했다.

ListView나 RecyclerView처럼 지원해주는 줄 알았다.

 

ScrollView는 간단하다.

LinearLayout처럼 ScrollView안에 들어갈 구성요소들을 감싸주면 된다.

 

여기서 주의사항은 두가지!

 

ScrollView 안에는 반드시 하나의 레이아웃을 둬야 한다.
<ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">

         <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="40dp"
                  android:gravity="bottom"
                  android:text="분류"/>

              <Spinner
             	  android:id="@+id/spinner1"
                  android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:spinnerMode="dialog"/>

              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="40dp"
                  android:gravity="bottom"
                  android:text="비고"/>
          </LinearLayout>

          <EditText
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_marginLeft="16dp"
              android:layout_marginRight="16dp"
              android:theme="@style/EditTextStyle"/>
              
          <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
        
      </LinearLayout>
      
    

</ScrollView>

이와 같이 많은 레이아웃을 정의하더라도 LinearLayout으로 감싸면 한개의 레이아웃이 된다. 

이 코드는 가능 하다.

 

 

하지만..

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

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">

         <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="40dp"
                  android:gravity="bottom"
                  android:text="분류"/>

              <Spinner
             	  android:id="@+id/spinner1"
                  android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:spinnerMode="dialog"/>

              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="40dp"
                  android:gravity="bottom"
                  android:text="비고"/>
          </LinearLayout>

          <EditText
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_marginLeft="16dp"
              android:layout_marginRight="16dp"
              android:theme="@style/EditTextStyle"/>
        
      </LinearLayout>
      
       <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />

</ScrollView>

이와 같이 FrameLayout과 LinearLayout 이 두개의 레이아웃으로 두면 오류가 난다.

ScrollView can host only one direct child Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child

처음처럼 하나의 레이아웃으로 만들면 오류 없이 잘 작동된다.

 

ScrollView 상단 코드는 보이지만 ScrollView 하단 코드는 보이지 않는다.

간단하다.

<Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="추가"/>
        
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        .....
        
    </LinearLayout>
</ScrollView>

<Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="저장"/>

이 코드를 실행했을 때 추가 버튼은 보이지만 저장 버튼은 보이지 않는다.

아무런 오류가 찍히지 않고 보이지 않기 때문에 원인을 찾기가 어렵다.

ScrollView 밖에 레이아웃을 만들고자 한다면 반드시 상단에 두자.