안드로이드 스튜디오 LinearLayout, RelativeLayout, Components

2019. 12. 13. 16:362020/Android App Develop

 

1. LinearLayout

Layout자체에서 요소들의 정렬 방향을 정할 수 있다. ( android:orientation="Vertical" or "Horizontal" )

화면 비중 또한 정할 수 있다. ( android:weightSum="" )

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
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
 
 
    <!-- 제일 첫 번째 도화지를 그린다. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="9"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="First Text"
            android:textColor="@android:drawable/edit_text"
 
            android:layout_weight="2"
            >
        </TextView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Second Text"
            android:textColor="@android:color/holo_green_dark"
 
            android:layout_weight="2"
            >
        </TextView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/tertiary_text_light"
            android:text="Third Text"
            android:layout_weight="5"
            >
        </TextView>
    </LinearLayout>
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

 

 

2. RelativeLayout

요소들 간의 관계를 직접정할 수 있다. 

CheckBox의 아이디값을 정해주었고, 이를 기준으로 두 요소들을 양 옆에 배치할 것이다.

관계를 정의하기 위해서 layout_toRightof, laout_toLeftof 를 사용

수직적으로 중앙에 위치시키기 위해서 centerVertical 사용

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
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!-- RelativeLayout (Full Screen) -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
 
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_toRightOf="@+id/checkBox"
            android:layout_centerVertical="true"
            />
 
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckBox"
            android:layout_centerInParent="true"
            android:background="@android:color/holo_blue_bright"
            />
 
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:layout_toLeftOf="@+id/checkBox"
            android:layout_centerVertical="true"
            />
 
    </RelativeLayout>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

 

 

요소를 양 옆이 아닌 위 아래에도 위치시킬 수 있다 ( layout_above, layout_below ) 이 경우에는 centerHorizontal를 통해 좌우 중앙을 맞추어야한다.

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
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!-- RelativeLayout (Full Screen) -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
 
 
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_below="@+id/checkBox"
            android:layout_centerHorizontal="true"
            />
 
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckBox"
            android:layout_centerInParent="true"
            android:background="@android:color/holo_blue_bright"
            />
 
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:layout_above="@+id/checkBox"
            android:layout_centerHorizontal="true"
 
            />
 
    </RelativeLayout>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter