자바 어플 개발/기초

ScrollView

appmaster 2019. 7. 11. 13:28

내가 지정한 뷰의 영역보다 내용물이 많을 때 만들어 줄 수 있다.

- 오직 단 하나의 자식만 가질 수 있다.

- 단 스크롤뷰의 자식이 다른 여러자식뷰를 가지는것은 상관이 없다. (어떤자식을 스크롤할지 고르기 때문이다.)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
        </LinearLayout>
        
        
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
        </LinearLayout>
        
        
        
    </ScrollView>
    
    
</LinearLayout>

이와같이 xml상에서 scrollview의 자식이 많아도 아무런 문제가 없다. 하지만 에러가 발생할때는 이 프로젝트를 실물 기계에 포팅하여서 돌릴때 그때 발생하게 된다.(포팅은 나중에 배운다)

 

속성

- fillViewport

스크롤뷰의 height를 설정할때 match_parent가 적용이 되지 않는다. 이유는 기본적으로 스크롤뷰 자체는 자식의 크기만큼 늘어나는 뷰이기 때문이다. 

 

True :  하지만 fillViewport값에 true를 주게된다면 하위 뷰의 크기만큼 늘어나게 된다.

 

False : (설명이 없었다.)

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

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


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent">

        </LinearLayout>



    </ScrollView>


</LinearLayout>

LinearLayout에 색깔만 넣으면 아무것도 보이지 않을것이다. 반드시 scrollview에 viewport속성을 true를 해야지 다 보이게 될 것이다.

 

 

- scrollbars

None = 스크롤바가 없다.

 

horizental = 가로에 스크롤바가 생긴다.

 

vertical = 세로에 스크롤바가 생긴다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:fillViewport="true">

        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/colorAccent">

            <TextView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_marginBottom="16dp"
                android:background="@color/colorPrimaryDark"/>

            <TextView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_marginBottom="16dp"
                android:background="@color/colorPrimaryDark"/>

            <TextView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_marginBottom="16dp"
                android:background="@color/colorPrimaryDark"/>

        </LinearLayout>



    </ScrollView>


</LinearLayout>

우변에 scrollbar가 이렇게 완성했음에도 당장 preview에는 보이지 않을 것이다. scrollbar는 사용자가 직접 손으로 할때  보이기 때문에 preveiw에서 확인할 수 없다. 실물기계에 포팅을 할때만 나타난다.

'자바 어플 개발 > 기초' 카테고리의 다른 글

강사와 나의 문제풀이 차이점  (0) 2019.07.12
FrameLayout  (0) 2019.07.11
RelativeLayout  (0) 2019.07.11
View의 계층구조  (0) 2019.07.11
LinearLayout  (0) 2019.07.11