티스토리 뷰

렐러티브 relative 란 ? 





렐러티브레이아웃 (상대 레이아웃)

  =>  렐러티브 레이아웃은 레이아웃 내부에 포함된 위젯들을 상대적인 위치로 배치

 



렐러티브레이아웃 함수

android:layout_above   => 위쪽

android:layout_toRightOf  => ~에 오른쪽 배치

android:layout alginParentRight => 부모의 오른쪽 배치 

 

렐러티브 레이아웃의 상하좌우에 배치가 가능

 

예제1




렐러티브 레이아웃을 이용하여 위와 같이 5개의 버튼과 함께 텍스트를 기입 해 보아라.










소스코드


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
37
38
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text"위쪽" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text"좌측" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text"중앙" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text"우측" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text"아래" />
 
 
 
</RelativeLayout>
 
cs

 






예제2




렐러티브 레이아웃을 이용하여 위그림같이 배치하여 보세요~!









소스코드


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
 
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
 
    </RelativeLayout>
 
    <Button
        android:id = "@+id/baseBtn"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text"기준위젯" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/baseBtn"
        android:layout_toLeftOf="@+id/baseBtn"
        android:text"1번" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/baseBBtn"
        android:layout_alignRight="@+id/baseBBtn"
        android:text"2번" />
    <Button
        android:id = "@+id/baseBBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/baseBtn"
        android:layout_toLeftOf="@+id/baseBtn"
        android:text"3번" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/baseBtn"
        android:layout_alignLeft="@+id/baseBtn"
        android:text"4번" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/baseBtn"
        android:layout_below="@+id/baseBtn"
        android:text"5번" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/baseBtn"
        android:layout_toRightOf="@+id/baseBtn"
        android:text"6번" />
 
 
 
</RelativeLayout>
cs



 

alignLeft 는 왼쪽을 기준으로 맞춘다는 것입니다.

반대로 alignRight는 오른쪽 기준이겠죠?



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   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