티스토리 뷰

  • 대 레이아웃이란?
다른 뷰나 부모 뷰와의 상대적인 위치를 이용해 뷰를 배치 하는 방법

서로 연결고리가 되어있다고 생각하면 된다.

  • Example_RelativeLayout 실습
1. xml 탭으로 들어가서 RelativeLayout 으로 바꿔준다.

2.제약 레이아웃이랑 비슷하다. (아무곳에나 붙일 수 있음.)
 
3. 버튼의 속성이 추가 된것을 확인 할 수 있다.
1
2
3
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"  // start도 사실상 특성과 left와 같다
cs
이렇게 하면 왼쪽 맨위쪽에 붙는것을 알수 있다.
이때 속성 layout_width = match_parent로 바꾸어준다.

4. 버튼 2 를 붙여준다.

첫번째 버튼의 아래쪽으로 붙여본다.
1
android:layout_below="@+id/button"
cs
속성중에 below가 추가 된것을 알 수 있다. 
below는 위쪽에 있는것을 아래쪽에 붙여주세요 라는뜻이다.
디자이너 도구 화면에서의 위젯을 끌어다 놓으면 id가 자동으로 생기게 된다.
이때, id값은 위쪽에 있는 부모의 아이디 값 넣어주게 되면 어떤것에 붙여졌는지 알 수 있다. 
1
"@+id/button"
cs
layout_width = match_parent
layout_height = match_parent 로 설정해주면

여기서 버튼3을 추가 시켜준다.


3번째 버튼의 크기를 layout_width 를 match_parent 설정해주면 3번째 크기도 오른쪽으로 쫙 붙게 된다. 

이때, 세번째버튼을 두번째 버튼 밑에다가 두고 싶을 때는

android:layout_above="@+id/button3" 이라고 붙여준다.

1
android:layout_above="@+id/button3"
cs
이때 완성되는 화면을 한번 보자. 우리가 일반적으로 볼 수 있는 레이아웃의 형태라고 생각을 할 수 있다. 중앙이 있고 아래와 위가 있는 형태의 레이아웃이다.
above라는 뜻은 ~보다 위에 있다라는 뜻이므로 버튼2는 버튼3보다 위에 있으므로 저렇게 나타내준다. 

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
<?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:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
 
       />
 
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button2"
 
         />
 
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button3" />
</RelativeLayout>
cs


이런것은 LinearLayout으로 짜면 매우 힘들다. relative Layout으로 짜는것이 좋다 이런형태일 경우에는 실제로 쓸때 매우 많이 사용된다.


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