«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

[위미르 개발팀] Android, iOS , Web 제작

[안드로이드 레이아웃] Visivility의 gone과 invisible의 차이 본문

개발자료/Android

[안드로이드 레이아웃] Visivility의 gone과 invisible의 차이

위미르개발팀 2017. 11. 16. 15:49

안드로이드에서 상황에 따라 뷰를 정의할때 어떠한 뷰는 보여야할때와 보이지 않아야할때가 있습니다.

이럴때 setVisibility에서 gone과 invisible을 사용하게 되는데, 이 둘을 사용을 하면서도 차이를 모르고 사용하는 분들이 많이 있습니다.


이 둘은 보이지 않는다는점은 같지만 차이는 뷰가 공간을 그대로 차지하고있느냐 완전히 없어지느냐 하는 것입니다.


RelativeLayout에서 다른 뷰를 기준으로 위치를 잡았을때 기준으로 잡은 뷰가 invisible일 경우 해당뷰에서 아무런 영향이 없지만, gone일 경우 기준이 된 뷰가 사라졌으므로 해당뷰의 위치가 변경됩니다.


그리고 LinearLayout에서도 horizontal 일때와 vertical 일때 모두 중간의 뷰가 invisible이면 빈공간이 생기지만, gone이 되면 해당 자리를 뒤의 뷰가 밀려가서 채워줍니다.


자신이 원하는대로 뷰를 구성하고싶다면 이 둘의 차이를 명확하게 이해하고, 사용할수 있어야 합니다.


LinearLayout 에서의 gone과 invisible의 차이 예제 입니다.



invisible 인 경우

view2는 안보이지만 자리를 지키고 있는 상태 입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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="com.wimir.MainActivity">
    <TextView
        android:text="VIEW1"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW2"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:visibility="invisible"/>
    <TextView
        android:text="VIEW3"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW4"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW5"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>
 
cs






gone 인 경우

view2는 안보이면서 사라져서  view3이 밀려올라온 상태입니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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="com.wimir.MainActivity">
    <TextView
        android:text="VIEW1"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW2"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:visibility="gone"/>
    <TextView
        android:text="VIEW3"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW4"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:text="VIEW5"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>
 
cs





희 위미르에서는 모바일 어플리케이션(Android/iOS), Web 개발을 해드리고 있습니다.


문의 전화 : 070-4177-3962


Comments