본문 바로가기
Android/Android

Android(3)_Layout

by 미티치 2016. 5. 27.

Layout 실습

 

 

 

1. 기능은 없지만 레이아웃 실습을 위한 예제입니다. 버튼마다 이벤트 처리해줘야하는데 이건 나중에..ㅎ.. 근데 layout_weight 비율이 왜 이렇게 들어가는지는 잘 모르겠지만.. (아마 화면 비율을 더 많이 차지하는 레이아웃이 숫자가 더 작은 것 같다)

-> layout_height = "0dp"로 주고 layout_weight를 화면에 보이는 만큼 숫자를 지정하면 (아래 예제코드에서 처럼 역비율로 주지않고) 된다!!

 

[ activity_main.xml ]

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mijinpark.a0527ex1.MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
 
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textArea"/>
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal">
 
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/buttonSave"
            android:text="@string/save"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/buttonCancel"
            android:text="@string/cancel"/>
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3">
 
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttonExit"
            android:text="@string/exit"/>
 
    </LinearLayout>
 
 
</LinearLayout>
cs

 

 

[ string.xml ]

1
2
3
4
5
6
7
<resources>
    <string name="app_name">0527Ex1</string>
    <string name="save">Save</string>
    <string name="cancel">Cancel</string>
    <string name="exit">Exit</string>
</resources>
 
cs

 

[ MainActivity.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.mijinpark.a0527ex1;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
cs

 

 

결과

 

 

 

 

 

2. 아직 소리기능은 넣지 않은 piano 뷰 소스입니다.

 

[ activity_main.xml ]

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/element"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.mijinpark.a0527piano.MainActivity">
 
    <!-- 흰건반 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
 
        <!-- 도 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/doo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 레 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/re"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 미 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/mi"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 파 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/pa"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 솔 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/sol"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 라 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/la"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000"></LinearLayout>
 
        <!-- 시 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20">
 
            <Button
                android:id="@+id/si"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:soundEffectsEnabled="false" />
        </LinearLayout>
 
        <!-- 줄 -->
    </LinearLayout>
    <!-- 검은건반 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:visibility="invisible"></LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">
 
                <Button
                    android:id="@+id/doS"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:soundEffectsEnabled="false" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:visibility="invisible"></LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">
 
                <Button
                    android:id="@+id/reS"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:soundEffectsEnabled="false" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="6"
                android:visibility="invisible"></LinearLayout>
 
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">
 
                <Button
                    android:id="@+id/paS"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:soundEffectsEnabled="false" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:visibility="invisible"></LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">
 
                <Button
                    android:id="@+id/solS"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:soundEffectsEnabled="false" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:visibility="invisible"></LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">
 
                <Button
                    android:id="@+id/laS"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:soundEffectsEnabled="false" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:visibility="invisible"></LinearLayout>
 
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:visibility="invisible"></LinearLayout>
 
    </LinearLayout>
</RelativeLayout>
 
 
cs

 

 

 

실행 결과

 

 

 

'Android > Android' 카테고리의 다른 글

Android(5)_Intent  (0) 2016.05.31
Android 계산기  (0) 2016.05.31
Android(4)_고급Widget  (0) 2016.05.30
Android(2)_Activity  (0) 2016.05.26
Android(1)_Structure  (0) 2016.05.26