«   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
Archives
Today
Total
관리 메뉴

[위미르 개발팀] Android, iOS , Web 제작

[안드로이드 레이아웃]WeightSum 사용하기 본문

개발자료/Android

[안드로이드 레이아웃]WeightSum 사용하기

위미르개발팀 2017. 9. 12. 10:13

안드로이드 개발을 하다보면 레이아웃에서 가장 난감한것이 layout_width와 layout_height값 입니다.


내폰에 맞게 높이 너비를 설정 했다고 해도 다른폰에서는 그렇게 나타나지 않을수가 있기때문입니다.


이를 해결하는 방법은 화면 높이 너비의 비율로 뷰의 높이 너비를 주는것입니다.


이를 지원하는 레이아웃은 LinearLayout이 있습니다.


LineartLayout은 선형 레이아웃으로 orientation을 vertical로 설정하면


LinearLayout


이런식으로 수직으로 뷰가 구성되고


orientation을 horizontal로 설정하면


LinearLayout

이런식으로 가로로 뷰가 구성됩니다.


LinearLayout의 속성에는 WeightSum 이라는것이 있는데, 이것이 뷰의 높이 너비를 비율로 설정할수 있게 해줍니다.


단, orientation이 vertical인 경우 높이만 가능하고, horizontal 의 경우 너비만 가능합니다.


예를들어서 부모 레이아웃인 LinearLayout의 WeightSum을 1로 설정하였고 orientation이 horizontal 입니다.


그렇다면 자식뷰들은 이를 이용하기 위해서, layout_width 값일 0dp로 설정하고 layout_weight를 0.5로 설정합니다.


그렇게되면 이 자식뷰는 부모레이아웃의 절반인 너비를 차지하게 됩니다.


자식뷰의 layout_weight 값은 부모 레이아웃의 WeightSum값을 100%로 보았을때 차지하는 비중입니다.


그리고 이것은 WeightSum 값을 설정한 LinearLayout의 1차 자식뷰에서만 해당이 됩니다.


2차 자식뷰에서도 이 속성을 이용하고 싶다면 1차 자식뷰를 LinearLayout으로 놓고 여기에 layout_weight값을 줌과 동시에


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
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
<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:layout_gravity="center_horizontal|center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:weightSum="1">
 
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:layout_weight="0.55"
        android:orientation="vertical"
        android:weightSum="1">
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:gravity="center" />
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="0.5"
            android:gravity="center" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.45"
        android:orientation="vertical"
        android:weightSum="1">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.15"
            android:orientation="horizontal"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:weightSum="1">
 
            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.1" />
 
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.4"
                android:gravity="center_vertical"
                android:paddingLeft="5dp" />
 
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:gravity="center_vertical|right" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.4"
            android:orientation="vertical"
            android:padding="10dp"
            android:weightSum="1">
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:orientation="horizontal"
                android:weightSum="1">
 
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.1" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="0.4"
                    android:gravity="center_vertical" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center_vertical" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:orientation="horizontal">
 
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.1" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="0.4"
                    android:gravity="center_vertical" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center_vertical" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:orientation="horizontal">
 
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.1" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="0.4"
                    android:gravity="center_vertical" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:gravity="center_vertical" />
            </LinearLayout>
        </LinearLayout>
 
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.02" />
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="5dp"
            android:layout_weight="0.2"
            android:orientation="horizontal">
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:padding="5dp"
                android:weightSum="1">
 
                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2" />
 
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.8"
                    android:gravity="center_vertical"
                    android:paddingLeft="8dp" />
            </LinearLayout>
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:paddingBottom="13dp"
            android:paddingTop="13dp"
            android:weightSum="1">
 
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.25" />
 
            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.25" />
 
            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.25" />
 
            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.25" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>







저희 위미르에서는 모바일 어플리케이션(Android/iOS), Web 개발을 해드리고 있습니다.


문의 전화 : 070-4177-3962







Comments