๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2023 ์•ˆ๋“œ๋กœ์ด๋“œ/์•ˆ๋“œ๋กœ์ด๋“œ ์Šคํ„ฐ๋””

๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ ๋งŒ๋“ค๊ธฐ

by hyeonha 2022. 11. 16.

๋ชฉ์ฐจ

    3. ํ…์ŠคํŠธ ์ปดํฌ์ €๋ธ”๋กœ ๊ฐ„๋‹จํ•œ ์•ฑ ๋นŒ๋“œ

    1. ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

    Jetpack Compose๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ์ƒ์ผ ์ถ•ํ•˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ฐ„๋‹จํ•œ android ์•ฑ์„ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.

    3. Happy Birthday ์•ฑ ์„ค์ •

    Empth Compose Activity ํ…œํ”Œ๋ฆฟ์œผ๋กœ Happy Birthday ์•ฑ์„ ๋งŒ๋“ค๋ฉด Android ์ŠคํŠœ๋””์˜ค์—์„œ๋Š” ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š”

    Hello Android! ๋ฉ”์‹œ์ง€๋ฅผ ๋น„๋กฏํ•˜์—ฌ ๊ธฐ๋ณธ Android ์•ฑ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์„ค์ •ํ•œ๋‹ค. 

     

           ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค(UI)๋ž€?

    ์•ฑ์—์„œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ž€ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€, ๋ฒ„ํŠผ, ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ์š”์†Œ ๋ฐ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋ฐฉ์‹

    ์ฆ‰, ์•ฑ์ด ์ฝ˜ํ…์ธ ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ์‹์ด์ž ์‚ฌ์šฉ์ž๊ฐ€ ์•ฑ๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

    ์ด๋Ÿฌํ•œ ๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ UI ๊ตฌ์„ฑ์š”์†Œ๋ผ๊ณ  ํ•œ๋‹ค. 

    ์•ฑ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์€ UI ์š”์†Œ์ด๋‹ค.

    UI์š”์†Œ๋Š” ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•œ ์š”์†Œ(EX. ํด๋ฆญ๊ฐ€๋Šฅํ•œ ๋ฒ„ํŠผ, ํŽธ์ง‘ ๊ฐ€๋Šฅํ•œ ์ž…๋ ฅ๋ž€) ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ  ์žฅ์‹์šฉ ์ด๋ฏธ์ง€๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค

     

    4. Jetpack Compose๋ž€? 

    Jetpack Compose๋Š” Android UI๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ตœ์‹  ํˆดํ‚ท์ด๋‹ค. 

    Compose๋Š” ์ ์€ ์–‘์˜ ์ฝ”๋“œ, ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ ๋ฐ ์ง๊ด€์ ์ธ Kotlin ๊ธฐ๋Šฅ์œผ๋กœ Android์—์„œ UI๊ฐœ๋ฐœ์„ ๊ฐ„์†Œํ™”ํ•˜๊ณ  ๊ฐ€์†ํ•œ๋‹ค.

    Compose ์‚ฌ์šฉ ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ UI์š”์†Œ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š ํ•จ์ˆ˜ , ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜(composable function)๋ผ๋Š” ํ•จ์ˆ˜ ์ง‘ํ•ฉ์„ ์ •์˜ํ•˜๋ฉฐ UI๋ฅผ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ composable function

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” Compose์—์„œ UI์˜ ๊ธฐ๋ณธ ๋นŒ๋“œ ๋ธ”๋ก์ด๋‹ค. 

      ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์˜ ํŠน์ง•

         - UI ์ผ๋ถ€๋ฅผ ์„ค๋ช…ํ•œ๋‹ค

         - ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

         - ๋ช‡ ๊ฐœ์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋‚ด์šฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

         - ์—ฌ๋Ÿฌ UI ์š”์†Œ๋ฅผ ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.

    ์ฃผ์„

     ์ฃผ์„์€ ์ฝ”๋“œ์— ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ฒจ๋ถ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Jetpack Compose ์ปดํŒŒ์ผ๋Ÿฌ์™€ ๊ฐ™์€ ๋„๊ตฌ ๋ฐ ๋‹ค๋ฅด๊ฐœ๋ฐœ์ž๊ฐ€ ์•ฑ์˜ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค.

     

    ์ฃผ์„์€ @๋ฌธ์ž๋ฅผ ์ด๋ฆ„์˜ ์ ‘๋‘์‚ฌ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ ์ ์šฉ๋œ๋‹ค. where? ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ์„ ์–ธ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—

    @Composable

     

    // Example code, do not copy it over
    
    @Json
    val imgSrcUrl: String
    
    @Volatile
    private var INSTANCE: AppDatabase? = null

    ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ฃผ์„

    ์ฃผ์„์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์ฃผ์„์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋„๊ตฌ์— ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 

    ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ฃผ์„
    ๋ฐฐ๊ฒฝ์„ ๋ฏธ๋ฆฌ ๋ณด์—ฌ์ฃผ๋Š” ์ฃผ์„
    ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ œ๋ชฉ์œผ๋กœ ์ฃผ์„ ๋‹ฌ๊ธฐ
    ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฃผ์„์— ์ „๋‹ฌ

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์˜ ์˜ˆ

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” @Composable ์ฃผ์„์œผ๋กœ ์ฃผ์„ ์ฒ˜๋ฆฌ ๋œ๋‹ค.  ๋ชจ๋“  ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์—๋Š” ์ด ์ฃผ์„์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

    ์ด ์ฃผ์„์„ ํ†ตํ•ด ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ UI๋กœ ๋ณ€ํ™˜ํ•˜๊ฒŒ ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ Compose ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค.

    cf) ์ปดํŒŒ์ผ๋Ÿฌ: ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„์”ฉ ๋ณด๊ณ  ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๊ณ„์–ด๋กœ ๋ณ€ํ™”ํ•˜๋Š” ํŠน์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

    @Composable
    fun Greeting(name: String) {
       Text(text = "Hello $name!")
    }

     

    ์ „๋‹ฌ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ name์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ํ…์ŠคํŠธ ์š”์†Œ๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๊ตฌ์„ฑ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์˜ ์˜ˆ

    ์ฐธ๊ณ ์‚ฌํ•ญ

     - ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ—ˆ์šฉํ•˜์—ฌ ์•ฑ ๋กœ์ง์ด UI๋ฅผ ์„ค๋ช…ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

    ์œ„์˜ ๊ฒฝ์šฐ UI์š”์†Œ๊ฐ€ String์„ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฆ„์„ ์ธ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

    - ํ•จ์ˆ˜๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. UI๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” UI ์š”์†Œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋Œ€์‹  ์›ํ•˜๋Š” ํ™”๋ฉด ์ƒํƒœใ…กใ„น ๊ธฐ์ˆ ํ•˜๋ฏ€๋กœ ๋ฐ˜ํ™˜ํ•  ๋‚ด์šฉ์ด ์—†๋‹ค.. ์ฆ‰, ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” UI๋ฅผ ๊ธฐ์ˆ ํ•˜์ง€๋งŒ UI๋ฅผ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฐ˜ํ™˜ํ•  ํ•ญ๋ชฉ์ด ์—†๋‹ค.

     

    ์ฝ”๋“œ์— ์žˆ๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ ํ™•์ธ

    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
        HappyBirthdayTheme {
            Greeting("Android")
        }
    }

     ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ BirthdayCardPreview() 

     

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. 

    ์ด ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์—์„œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํ•จ์ˆ˜๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ Greeting() ์„ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค. 

     

    ์ฝ”๋“œ ์Šค๋‹ˆํŽซ ???

    ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ?? ๋ฌด์Šจ ๋ง์ด์ง€ ?? 

     

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ ์ด๋ฆ„ 

    ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ @Composable ์ฃผ์„์„ ๋‹ฌ๊ณ  ์žˆ๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” ํŒŒ์Šค์ปฌ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฆ„ ์ง€์ •

    ํŒŒ์Šค์นผ ํ‘œ๊ธฐ๋ฒ•: ๋ณตํ•ฉ์–ด์—์„œ ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๋Š” ๋ช…๋ช… ๊ทœ์น™ํŒŒ์Šค์นผ ํ‘œ๊ธฐ๋ฒ•๊ณผ ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ• ์˜ ์ฐจ์ด ํŒŒ์Šค์นผ ํ‘œ๊ธฐ๋ฒ•์€ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•์€ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋Š” ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐ x

     

    Composeํ•จ์ˆ˜ ์ด๋ฆ„์˜ ํŠน์ง•

     

    5. Android ์ŠคํŠœ๋””์˜ค์˜ Design ์ฐฝ

    ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์ œ๊ณตํ•ด์•ผํ•œ๋‹ค. 

    Greeting() ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋ฏธ๋ฆฌ ๋ณผ ์ˆ˜ ๋Š” ์—†๋‹ค. ๋Œ€์‹  ์ ์ ˆํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Greeting()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” BirthdayCardPreview()์ด๋‹ค .

    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
       BirthdayCardTheme {
           Greeting("Android")
       }
    }
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
        HappyBirthdayTheme {
            Greeting("James")
        }
    }

     

    6. ์ƒˆ ํ…์ŠคํŠธ ์š”์†Œ ์ถ”๊ฐ€ 

    package com.example.happybirthday
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.tooling.preview.Preview
    import com.example.happybirthday.ui.theme.HappyBirthdayTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                HappyBirthdayTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(color = MaterialTheme.colors.background){
                }
                }
            }
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String) {
        Text(text = message)
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
        HappyBirthdayTheme {
            BirthdayGreetingWithText(message = "Happy Birthday Sam!")
        }
    }

    7. ๊ธ€๊ผด ํฌ๊ธฐ ๋ณ€๊ฒฝ

     

    ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํ”ฝ์…€ (SP)๋Š” ๊ธ€๊ผด ํฌ๊ธฐ์˜ ์ธก์ • ๋‹จ์œ„

    Android ์•ฑ์˜ UI ์š”์†Œ๋Š” ๋‘ ๊ฐ€์ง€ ์ธก์ • ๋‹จ์œ„๋ฅผ ์‚ฌ์šฉ

    1) DP : density independent pixel : ๋ ˆ์ด์•„์›ƒ์— ์‚ฌ์šฉ

    2) SP : scalable pixels : DP๋‹จ์œ„์™€ ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•˜์ง€๋งŒ, ์‚ฌ์šฉ์ž๊ฐ€ ํœด๋Œ€์ „ํ™” ์„ค์ •์—์„œ ์„ ํƒํ•œ ์„ ํ˜ธํ•˜๋Š” ํ…์ŠคํŠธ ํฌ๊ธฐ์— ๋”ฐ๋ผ ํฌ๊ธฐ๊ฐ€ ์กฐ์ ˆ๋œ๋‹ค. 

     

     

    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.sp
    import com.example.happybirthday.ui.theme.HappyBirthdayTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                HappyBirthdayTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(color = MaterialTheme.colors.background){
                }
                }
            }
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String) {
        Text(
            text = message,
            fontSize = 36.sp
        )
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
        HappyBirthdayTheme {
            BirthdayGreetingWithText(message = "Happy Birthday Sam!")
        }
    }

     

    8. ๋‹ค๋ฅธ ํ…์ŠคํŠธ ์š”์†Œ ์ถ”๊ฐ€

    ์ด๋ฆ„์œผ๋กœ ์นด๋“œ์— ์„œ๋ช…ํ•˜๋Š” ์ž‘์—…์„ ํ•ด๋ณด์ž

    ์ˆ˜์ • ํ•œ ๋ถ€๋ถ„

    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
        Text(
            text = message,
            fontSize = 32.sp,
        )
        Text(
            text = from,
            fontSize = 24.sp
        )
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }

    Text ์ถ”๊ฐ€ํ•  ๋•Œ ์œ„์— ์žˆ๋Š” ๊ธฐ์กด์˜ Text ๋ถ€๋ถ„์— from์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ƒˆ๋กœ Text()๋ฅผ ๋งŒ๋“ค์–ด์„œ  ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.sp
    import com.example.happybirthday.ui.theme.HappyBirthdayTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                HappyBirthdayTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(color = MaterialTheme.colors.background){
                }
                }
            }
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
        Text(
            text = message,
            fontSize = 32.sp,
        )
        Text(
            text = from,
            fontSize = 24.sp
        )
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardPreview() {
        HappyBirthdayTheme {
            BirthdayGreetingWithText(message = "Happy Birthday Sam!", "- from Emma")
        }
    }

     

    9. ํ–‰๊ณผ ์—ด์— ํ…์ŠคํŠธ ์š”์†Œ ์ •๋ ฌ

    UI ๊ณ„์ธต ๊ตฌ์กฐ๋Š” ํฌํ•จ์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค.

    ์ฆ‰, ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ƒ์œ„ ์š”์†Œ ๋ฐ ํ•˜์œ„ ์š”์†Œ๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

    context๋Š” ์ƒ์œ„ UI์š”์†Œ๊ฐ€ ํ•˜์œ„ UI์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ด ํ•˜์œ„ UI์š”์†Œ๋Š” ํ•˜์œ„ UI์š”์†Œ๋ฅผ ์ฐจ๋ก€๋กœ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

    Compose์˜ ์„ธ ๊ฐ€์ง€ ๊ธฐ๋ณธ ํ‘œ์ค€ ๋ ˆ์ด์•„์›ƒ ์š”์†Œ

    1) column composable

    2) row composable

    3) box composable

     

    column, row, boxsms composable ์ฝ˜ํ…์ธ ๋ฅผ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” composable function์ด๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๋ ˆ์ด์•„์›ƒ ์š”์†Œ ๋‚ด์— ํ•ญ๋ชฉ์„ ๋ฐฐ์น˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

     

    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
        Row {
            Text("First column")
            Text("Second column")
        }
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }

    ์ด ๋ถ€๋ถ„๋งŒ ์ˆ˜์ •ํ•ด์ฃผ๊ณ  refresh & build ํ•ด์ฃผ์—ˆ๋‹ค.

    ํ›„ํ–‰ ๋žŒ๋‹ค ๋ฌธ๋ฒ• -> ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด 

    Row์— ๊ด„ํ˜ธ ๋Œ€์‹  ์ค‘๊ด„ํ˜ธ๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. ์ด๋ฅผ ํ›„ํ–‰ ๋žŒ๋‹ค ๋ฌธ๋ฒ•์ด๋ผ๊ณ  ํ•œ๋‹ค.

    ์ผ๋‹จ compose ๋ฌธ๋ฒ•์— ์ต์ˆ™ํ•ด์ง€๊ธฐ! 

    Kotlin์€ ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜์ผ ๋•Œ ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ํŠน์ˆ˜ํ•œ ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

    ํ•จ์ˆ˜๋ฅผ ์ด๋Ÿฌํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ ค๋ฉด ํ›„ํ–‰ ๋žŒ๋‹ค ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

    ํ•จ์ˆ˜๋ฅผ ๊ด„ํ˜ธ์•ˆ์— ํ•จ์ˆ˜ ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ๋„ฃ๋Š” ๋Œ€์‹  ํ•จ์ˆ˜ ์ด๋ฆ„ ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋„ฃ๋Š”๋‹ค. 

    Row()์˜ ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํ•˜์œ„  UI์š”์†Œ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ธ content ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค.

    Row(
        content = {
            Text("Some text")
            Text("Some more text")
            Text("Last text")
        }
    )

    content ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ์„œ๋ช…์˜ ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋ฉฐ ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค.

    ๋”ฐ๋ผ์„œ content ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ด„ํ˜ธ๋ฅผ ์‚ญ์ œ ๊ฐ€๋Šฅ ??

    Row {
        Text("Some text")
        Text("Some more text")
        Text("Last text")
    }

    ํ•˜๋‚˜์˜ ํ–‰์— ํ…์ŠคํŠธ ์š”์†Œ ์ •๋ ฌ

    ํ…์ŠคํŠธ ์š”์†Œ๊ฐ€ ๊ฒน์น˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์•ฑ์˜ ํ…์ŠคํŠธ ์š”์†Œ๋ฅผ ํ•œ ํ–‰์— ์ •๋ ฌ

    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
        Row{
            Text(
                text = message,
                fontSize = 32.sp,
            )
            Text(
                text = from,
                fontSize = 24.sp
            )
        }
        // ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” Text() ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    }

    ๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

    ๋‘ ๊ฐœ์˜ ํ…์ŠคํŠธ์š”์†Œ๊ฐ€ ์žˆ๋Š” ์—ด์„ ํ‘œ์‹œํ•˜๋„๋ก ํ…์ŠคํŠธ ์š”์†Œ ์ฃผ์œ„์— Row composable์„ ์ถ”๊ฐ€ํ•œ๋‹ค. 

     

    ์ด์ œ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์€ ์—†๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ช…ํ•  ๊ณต๊ฐ„์ด ์ถฉ๋ถ„์น˜ ์•Š๋‹ค. 

    ํ…์ŠคํŠธ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์˜ ์—ด์— ์ •๋ ฌํ•ด๋ณด์ž

     

    ํ•˜๋‚˜์˜ ์—ด์— ํ…์ŠคํŠธ ์š”์†Œ ์ •๋ ฌ

    ์œ„ ์ฝ”๋“œ์—์„œ Row๋ฅผ Column์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.

    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
       Column {
           Text(
               text = message,
               fontSize = 36.sp,
           )
           Text(
               text = from,
               fontSize = 24.sp,
           )
       }
    }

    10. ๊ธฐ๊ธฐ์— ํ‘œ์‹œํ•˜๊ธฐ

    ํ•จ์ˆ˜๋กœ ์Šคํฌ๋กคํ•˜๊ธฐ ๊ฐ€ ๋ญ˜ ํ•˜๋ผ๋Š” ๊ฑธ๊นŒ์š” ??

    ์–ด ๋˜ ๊ถ๊ธˆํ•œ ๊ฒŒ  xml ๋ง๊ณ  jetpack compose์œผ๋กœ ํ•˜์ž–์•„์š”

    ๊ทผ๋ฐ xml์œผ๋กœ ๊ณต๋ถ€๋ฅผ ํ•  ๋–„๋Š” ํ•ญ์ƒ empty activity๋ฅผ ๋งŒ๋“ค์–ด ์คฌ๋Š”๋ฐ 

    compose๋กœ ํ•˜๋‹ˆ๊น empty compose activity๋ฅผ ๋งŒ๋“ค๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”???

    jetpack compose๋ฅผ ์‚ฌ์šฉํ• ๋ ค๋ฉด ํ•ญ์ƒ empty compose activity๋กœ ์ƒ์„ฑํ•ด์ค˜์•ผํ•˜๋Š”๊ฑด๊ฐ€์š”? 

     

    ์š”์•ฝ

    1) Jetpack Compose๋Š” Android UI๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ตœ์‹  ๋„๊ตฌ ํ‚ค๋“œ์ด๋‹ค.

    Jetpack Compose๋Š” ๋” ์ ์€ ์ˆ˜์˜ ์ฝ”๋“œ, ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ, ์ง๊ด€์ ์ธ Kotlin API๋กœ Android ์—์„œ์˜ UI๊ฐœ๋ฐœ์„ ๊ฐ„์†Œํ™” & ๊ฐ€์†ํ™”

    2) ์•ฑ์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค (UI)๋Š” ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€, ๋ฒ„ํŠผ, ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ์š”์†Œ์ด๋‹ค.

    3) ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ composable ํ•จ์ˆ˜๋Š” Compose์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ ์š”์†Œ์ด๋‹ค. ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” UI์˜ ์ผ๋ถ€๋ฅผ ์„ค๋ช…ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

    4) ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” @Composable ์ฃผ์„์œผ๋กœ ์ฃผ์„์ฒ˜๋ฆฌ๋œ๋‹ค. ์ด ์ฃผ์„์€ ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ UI๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์„ Compose ์ปดํŒŒ์ผ๋Ÿฌ์— ์•Œ๋ ค์ค€๋‹ค

    5) Compose์˜ ์„ธ ๊ฐ€์ง€ ํ‘œ์ค€ ๋ ˆ์ด์•„์›ƒ ์š”์†Œ๋Š” Column, Row, Box ์ด๋‹ค

    ์ด๋Ÿฌํ•œ ์š”์†Œ๋Š” ์ปดํฌ์ €๋ธ” ์ฝ˜ํ…์ธ ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์ด๋ฏ€๋กœ ๋‚ด๋ถ€์— ํ•ญ๋ชฉ์„ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

    Row ๋‚ด์˜  ๊ฐ ํ•˜์œ„ ์š”์†Œ๋Š” ๊ฐ€๋กœ๋กœ ๋‚˜๋ž€ํžˆ ๋ฐฐ์น˜๋œ๋‹ค. 

    728x90