๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Android & Kotlin

Lesson 2: Layouts

Udacity ์—์„œ Developing Android Apps with Kotlin ์ด๋ผ๋Š” ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  lesson ๋ณ„๋กœ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์„ ํ•„๊ธฐํ•œ ๋‚ด์šฉ์ด๋‹ค. 
์˜์–ด ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ํ•„๊ธฐํ•œ ๋‚ด์šฉ์ด๋‹ค ๋ณด๋‹ˆ ๋ฒˆ์—ญ์ด๋‚˜ ํ•ด์„์ด ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋˜ ์ผ๋ถ€๋Š” ์˜์–ด ๋‹จ์–ด๋‚˜ ๋ฌธ์žฅ์„ ๊ทธ๋Œ€๋กœ ์ ์–ด๋†“์•˜๋‹ค. 
์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ฐ•์˜๋ฅผ ์ง์ ‘ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•œ๋‹ค.

 

Developing Android Apps with Kotlin | Udacity Free Courses

Take Udacity's free online course and learn Android development with Kotlin. Master the fundamentals of the Kotlin programming language taught by Kotlin experts at Google.

www.udacity.com


3. View Groups & View Hierarchy

dp: location๊ณผ dimension์„ ๋‚˜ํƒ€๋‚ด๋Š” unit(๋‹จ์œ„)์œผ๋กœ density independent pixel์˜ ์•ฝ์–ด์ด๋‹ค. ๋ฌผ๋ฆฌ์  ์Šคํฌ๋ฆฐ ํ”ฝ์…€์˜ ๋ฐ€๋„์— ๊ทผ๊ฑฐํ•˜๋ฉฐ ์•ˆ๋“œ๋กœ์ด๋“œ ๋””๋ฐ”์ด์Šค๋Š” ์ž๋™์œผ๋กœ dp โžก๏ธ px ์ปจ๋ฒ„์ „์„ ํ†ตํ•ด ๊ฐ’์„ ํ•ธ๋“ค๋งํ•œ๋‹ค. 
160 dpi ์Šคํฌ๋ฆฐ์—์„œ 1dp = 1 pixel, 480 dpi ์Šคํฌ๋ฆฐ์—์„œ 1dp = 3 pixels

View Groups

  • LinearLayout: horizontal / Vertical
  • ScrollView: For scrollable view
  • ConstraintLayout: view hierarchy์˜ ๊นŠ์ด๊ฐ€ ๊นŠ์„์ˆ˜๋ก ๋ ˆ์ด์•„์›ƒ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋” ๋งŽ์€ work์ด ํ•„์š”ํ•˜๋‹ค. big & deepํ•œ view์˜ ๊ณ„์ธต ๊ตฌ์กฐ๋Š” ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ๋„ ๋ถˆํŽธ์„ ์ดˆ๋ž˜ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์•ˆ๋“œ๋กœ์ด๋“œ๋Š” ConstraintLayout์„ ์ œ๊ณตํ•˜๋Š”๋ฐ flatํ•œ  hierarchy๋ฅผ View๋ฅผ promoteํ•˜๊ณ  view๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š”๋ฐ ์ตœ์ ํ™”๋˜์–ด ์žˆ๋‹ค. ๋ณต์žกํ•œ layout์ธ๋ฐ ์ ์€ ์ˆ˜์˜ View๋‚˜ ViewGroup์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค. 

7. Adding a TextView

sp: scale independent pixels, dp์™€ ๋น„์Šทํ•˜์ง€๋งŒ ํ™”๋ฉด์˜ pixel density ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ธ๋“œํฐ์— ์„ธํŒ…ํ•œ ํฐํŠธ ์‚ฌ์ด์ฆˆ๋„ ๋ฐ˜์˜ํ•˜์—ฌ scaleํ•จ. 

setContentView()์ด ์ฝ”๋“œ๊ฐ€ layout๊ณผ activity๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„. 

10. Exercise: Add a ScrollView

ScrollView๋Š” ํ•œ ๊ฐœ์˜ View๋‚˜ ViewGroup๋งŒ ์ž๋…€๋กœ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. ๋ณดํ†ต ๋‚ด๋ถ€์— LinearLayout์„ ๋‹ด๊ณ  LinearLayout ์•ˆ์— ๋˜ ๋‹ค๋ฅธ View๋“ค์„ ๋‹ด๋Š”๋‹ค. 

11. Adding an EditText for Text Input

  • EditText๋Š” TextView์˜ ์ž๋…€ ํด๋ž˜์Šค์ด๋‹ค.
  • ์•ˆ๋“œ๋กœ์ด๋“œ ์‹œ์Šคํ…œ์€ inputType์— ๋”ฐ๋ผ์„œ input๊ฐ’์„ validateํ•œ๋‹ค. 
  • ๊ฐ€์žฅ ์ข์€ inputType์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž๋“ค์ด ์—๋Ÿฌ๋ฅผ ํ”ผํ•˜๋Š”๋ฐ ๋„์›€์ด ๋œ๋‹ค. 

12. Adding a Done button to Accept Text Input

Visibility ์†์„ฑ ์ค‘ invisible / Gone ๋ชจ๋‘ text๋ฅผ ์ˆจ๊ธฐ์ง€๋งŒ invisible์€ ๊ทธ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ณ , gone์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

  • Button์€ TextView๋ฅผ ํ™•์žฅํ•œ ๊ฒƒ์ด๋‹ค.
  • ์‚ฌ์šฉ์ž๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ, ๋ˆ„๋ฅด๋ฉด ๋ฌด์–ธ๊ฐ€๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. 

13. Exercise: Add EditText, Done Button, ClickHandler

// Hide the keyboard
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)

 

14. Data Binding

๋งค๋ฒˆ findViewById์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์•ˆ๋“œ๋กœ์ด๋“œ๊ฐ€ view hierarchy๋ฅผ runtime์— ํƒ์ƒ‰ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ํฌ๊ณ  ๊นŠ์€ view hierarchy๋Š” ์•ฑ์ด ๋Š๋ ค์ง€๋Š” ์š”์ธ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ์— Data Binding์ด ์œ ์šฉํ•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด layout๊ณผ activity๋ฅผ compile time์— ์—ฐ๊ฒฐํ•˜๊ฒŒ ํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” helper class์ธ Binding Class๋ฅผ activity๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ์ถ”๊ฐ€์ ์ธ overhead ์—†์ด view์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. (View์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ๋งŒ ํ•„์š”ํ•˜๋‹ค๋ฉด view binding์„, data binding์ด ์ œ๊ณตํ•˜๋Š” ๋” ๊ณ ๊ธ‰์˜ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด data binding์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. )

15. Data Binding: Views

  • ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์€ ๋ ˆ์ด์•„์›ƒ ๋‚ด์— ์žˆ๋Š” ๋ชจ๋“  view์— ๋Œ€ํ•ด์„œ ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ๋ฐ”์ธ๋”ฉ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 
  • ๋ฐ”์ธ๋”ฉ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด view์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด findViewById๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.
  • findViewById ํƒ์ƒ‰์€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒํ•œ๋‹ค.

16. Data Bind: Data

19. ConstraintLayout: ColorMyViews App

ConstraintLayout์˜ ์ด์ 

  • ์Šคํฌ๋ฆฐ๊ณผ ํ•ด์ƒ๋„์— ๋งž์ถฐ responsiveํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ๋Œ€๊ฐœ flatter view hierarchy๋ฅผ ๊ฐ–๋Š”๋‹ค.
  • view๋“ค์˜ ๋ฐฐ์น˜์— ์ตœ์ ํ™”๋œ ๋ ˆ์ด์•„์›ƒ
  • Free-form: ์–ด๋””์— ๋ฐฐ์น˜ํ•ด๋„ ์—๋””ํ„ฐ๊ฐ€ ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค€๋‹ค. 

22. Constraints

23. Ratio

Ratio๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด

  • view์˜ width๋‚˜ height๋ฅผ 0dp๋กœ ์„ค์ •ํ•˜์—ฌ ๊ณ„์‚ฐ๋  ๊ฒƒ์ž„์„ ์•Œ๋ ค์คŒ.
  • dimension ratio๋ฅผ ์„ค์ •.

24. Chaining

์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด chain.

 

'Android & Kotlin' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Lesson 5: App Architecture(UI Layer)  (0) 2022.02.22
๊ณต๋ถ€ ์ฃผ์ œ  (0) 2022.02.19
Lesson 4: Activity & Fragment Lifecycle  (0) 2022.02.11
Lesson 3: App Navigation  (0) 2022.02.08
Lesson 1: Build your First App  (0) 2022.02.06