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

์ฒซ Android ์•ฑ ๋งŒ๋“ค๊ธฐ

by hyeonha 2022. 11. 15.

๋ชฉ์ฐจ

    6. ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ ๋ณ€๊ฒฝ

    import ์‹œ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. 

    help - find action - Optimize Imports ํด๋ฆญ

     

    @Composable
    fun Greeting(name: String) {
        Surface(color = Color.Magenta) {
            Text(text = "Hello my name is $name!")
        }
    }

    ํ…์ŠคํŠธ์— ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ์ด ์ƒ๊ฒผ๋‹ค.

    7. ํŒจ๋”ฉ ์ถ”๊ฐ€ 

    ํ…์ŠคํŠธ ์ฃผ์œ„์— ๊ณต๋ฐฑ(ํŒจ๋”ฉ)์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž

    @Composable
    fun Greeting(name: String) {
       Surface(color = Color.Magenta) {
           Text(text = "Hi, my name is $name!", modifier = Modifier.padding(24.dp))
       }
    }

    Modifier๋Š” ์ปดํฌ์ €๋ธ”์„ ๊ฐ•ํ™”ํ•˜๊ฑฐ๋‚˜ ์žฅ์‹ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

    ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ๊ฐ€์ง€ ์ˆ˜์ •์ž๋Š” padding ์ˆ˜์ •์ž์ด๋ฉฐ  ์ด๋Š” ์š”์†Œ ์ฃผ์œ„์— ๊ณต๋ฐฑ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

    ์—ฌ๊ธฐ์„œ๋Š” ํ…์ŠคํŠธ ์ฃผ์œ„์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 

     

    @Composable
    fun Greeting(name: String) {
        Surface(color = Color.Magenta) {
            Text(text = "Hello my name is $name!", Modifier.padding(24.dp))
        }
    }

    ์†”๋ฃจ์…˜ ์ฝ”๋“œ

    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.layout.padding
    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.graphics.Color
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.dp
    import com.example.myapplication.ui.theme.GreetingCardTheme
    
    class MainActivity : ComponentActivity() {
       override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContent {
               GreetingCardTheme {
                   // A surface container that uses the 'background' color from the theme
                   Surface(color = MaterialTheme.colors.background) {
                       Greeting("Android")
                   }
               }
           }
       }
    }
    
    @Composable
    fun Greeting(name: String) {
       Surface(color = Color.Magenta) {
           Text(text = "Hi, my name is $name!", modifier = Modifier.padding(24.dp))
       }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview() {
       GreetingCardTheme {
           Greeting("Meghan")
       }
    }

    ์š”์•ฝ

    ๊ตฌ์„ฑ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

    ํ•จ์ˆ˜ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐ๋˜๋ฉฐ ํ•จ์ˆ˜ ์•ž์— @Composable ์ฃผ์„์„ ์ถ”๊ฐ€ํ•˜๊ณ  

    @Composable ํ•จ์ˆ˜๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค.

    Modifier์€ ์ปดํฌ์ €๋ธ”์„ ๊ฐ•ํ™”ํ•˜๊ฑฐ๋‚˜ ์žฅ์‹ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. 

    728x90