티스토리 뷰

 테이블 레이아웃 (TableLayout)


- 주로 위젯을 표 형태로 배치할때 사용

- <TableRow> 와 함께 사용되는데 <TableRow>의 개수가 바로 행의 개수가 됨

- 열의 개수는 <TableRow> 안에 포함된 위젯의 개수로 결정



테이블레이아웃의 속성


layout_column : 지정된 열에 현재 위젯을 표시 하라는 의미

stretchColumns:  지정된 열의 폭을 늘리라는 의미

stretchColumns = '*' 로 하면 각 셀을 모두 같은 크기로 확장해서 전체 화면이 꽉 차는 효과를 갖는다.



테이블레이아웃을 활용하여 숫자 버튼까지 있는 계산기를 만들어 보자. 



소스코드


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:stretchColumns="*" android:layout_width="match_parent" android:layout_height="match_parent">
 
 
    <EditText
        android:id="@+id/num1"
        android:hint="숫자1 입력"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/num2"
        android:hint="숫자2 입력"
        android:textSize="20dp"/>
 
    <TableRow>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn0"
            android:text="0"/>
        <Button
           android:layout_weight="1"
            android:id="@+id/btn1"
            android:text="1"
           /> android:layout_span="10"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn2"
            android:text="2"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn3"
            android:text="3"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn4"
            android:text="4"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn5"
            android:text="5"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn6"
            android:text="6"
            /> android:layout_span="10"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn7"
            android:text="7"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn8"
            android:text="8"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn9"
            android:text="9"/>
    </TableRow>
    <Button
        android:id="@+id/btnAdd"
        android:text="더하기"
        android:textSize="20dp"
        android:layout_margin="5dp"/>
    <Button
        android:id="@+id/btnSub"
        android:text="빼기"
        android:textSize="20dp"
        android:layout_margin="5dp"/>
    <Button
        android:id="@+id/btnMulti"
        android:text="곱하기"
        android:textSize="20dp"
        android:layout_margin="5dp"/>
    <Button
        android:id="@+id/btnDiv"
        android:text="나누기"
        android:textSize="20dp"
        android:layout_margin="5dp"/>
 
    <TextView
        android:textSize="30dp"
        android:text="계산결과 : "/>
 
</TableLayout>
cs



숫자버튼들을 테이블 레이아웃을 활용하였다. 


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
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