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

Unit1- Jetpack Compose๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

by hyeonha 2022. 12. 24.

๋ชฉ์ฐจ

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

    Compose๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ UI์š”์†Œ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ธ Composable ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ UI๋ฅผ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค. 

    compoable ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ @Composable ์ฃผ์„์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

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

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

    compoable ํ•จ์ˆ˜

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

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

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

     

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

     

    @Composable
    fun Greeting(name: String) {
        Text(text = "Hello $name!")
    }
    
    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview() {
        BirthdayCardTheme {
            Greeting("Android")
        }
    }

    composable ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ composable ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. 

     

    composable ํ•จ์ˆ˜์˜ ์ด๋ฆ„ : ํŒŒ์Šค์นผ ํ‘œ๊ธฐ๋ฒ• : ๋ณตํ•ฉ์–ด์—์„œ ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐ (vs ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•: ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋Š” ๋Œ€๋ฌธ์ž๋กœ ์“ฐ์ง€ ์•Š์Œ)

     

    Design์ฐฝ

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

    Greeting () ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋ฏธ๋ฆฌ ๋ณผ ์ˆ˜ ๋Š” ์—†๋‹ค!   ->>>>>>>>>. ์™€์ด?  

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

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

    [์‹ค์Šต ]

    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.birthdaycard.ui.theme.BirthdayCardTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                BirthdayCardTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colors.background
                    ) {
                    }
                }
            }
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String) {
        Text(
            text = message
        )
    }
    
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardTPreview() {
        BirthdayCardTheme {
            BirthdayGreetingWithText(message = "Happy Birthday sunghyeon!")
        }
    }

    ๊ธ€๊ผด ํฌ๊ธฐ ๋ณ€๊ฒฝํ•˜๊ธฐ 

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

    1) dp  : ๋ ˆ์ด์•„์›ƒ์— ์‚ฌ์šฉ

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

     

     

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

    Text( ) ์ถ”๊ฐ€

     

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

    UI ๊ณ„์ธต ๊ตฌ์กฐ

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

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

    ์ด ์„น์…˜์—์„œ ์ƒ์œ„ UI ์š”์†Œ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์—ด , ํ–‰ , box  composable์— ๊ด€ํ•ด ์•Œ์•„๋ณด์ž 

     

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

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

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

    Row {
    	Text (" First " )
        Text (" Second " )
        }

     

    ํ›„ํ–‰ ๋žŒ๋‹ค ๋ฌธ๋ฒ• -> ???????????

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

     

    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")
    }

     

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

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

     

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

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

    MainActivity.kt ํŒŒ์ผ์—์„œ onCreate ํ•จ์ˆ˜๋กœ ์Šคํฌ๋กค 

    Surface ๋ธ”๋ก์—์„œ BirthdayGreetingWithText() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    BirthdayGreetingWithText() ํ•จ์ˆ˜, ์ƒ์ผ ์ถ•ํ•˜ ์ธ์‚ฌ๋ง ๋ฐ ์„œ๋ช…ใ…‡๋ฅด ์ „๋‹ฌ

    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                BirthdayCardTheme {
                    // A surface container using the 'background' color from the theme
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colors.background
                    ) {
                        BirthdayGreetingWithText(message = "Happy Birthday hyeon!" , " - from = zu")
                    }
                }
            }
        }
    }

    12. ๊ฒฐ๋ก 

    ์š”์•ฝ 

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

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

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

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

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

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

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

    728x90