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

HappyBirthdayCard ์ฝ”๋“œ

by hyeonha 2022. 12. 24.

๋ชฉ์ฐจ

    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.Image
    import androidx.compose.foundation.layout.*
    import androidx.compose.material.MaterialTheme
    import androidx.compose.material.Surface
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Alignment
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.layout.ContentScale
    import androidx.compose.ui.res.painterResource
    import androidx.compose.ui.res.stringResource
    import androidx.compose.ui.tooling.preview.Preview
    import androidx.compose.ui.unit.dp
    import androidx.compose.ui.unit.sp
    import com.example.birthdaycard.ui.theme.BirthdayCardTheme
    import kotlinx.coroutines.NonCancellable
    
    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
                    ) {
                        BirthdayGreetingWithImage(stringResource(R.string.happy_birthday_text) , stringResource(
                                                R.string.singature_text))
                    }
                }
            }
        }
    }
    
    @Composable
    fun BirthdayGreetingWithText(message: String, from: String) {
        Column (horizontalAlignment = Alignment.CenterHorizontally){
            Text(
                text = message,
                fontSize = 36.sp,
                modifier = Modifier
                    .fillMaxWidth() // ์ปดํฌ์ €๋ธ”์˜ ๋„ˆ๋น„๊ฐ€ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ๋„ˆ๋น„๋กœ ์„ค์ •๋œ๋‹ค.
                    .wrapContentWidth(Alignment.Start)
                    .padding(start = 16.dp,end=16.dp)
                // wrapContentWidth : ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ํ™”๋ฉด ๋„ˆ๋น„(๋˜๋Š” ์ƒ์œ„ ๋„ˆ๋น„)์— ๋งž์ถฐ ์ปดํฌ์ €๋ธ”์ด ๋ž˜ํ•‘๋˜๊ณ  ์ •๋ ฌ๋œ๋‹ค.
                // Alignment.Start : ํ…์ŠคํŠธ ์ปดํฌ์ €๋ธ” ๋˜๋Š” ํ•˜์œ„ ์š”์†Œ๊ฐ€ ํ™”๋ฉด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„ ๋˜๋Š” ์ƒ์œ„ ์š”์†Œ์— ๋งž์ถฐ ์ •๋ ฌ๋œ๋‹ค.
    
    
            )
            Text(
                text = from,
                fontSize = 24.sp,
            )
    
        }
    }
    
    @Composable
    fun BirthdayGreetingWithImage(message: String, from: String) {
        val image = painterResource(R.drawable.androidparty)
        Box {
            Image(painter = image,
                contentDescription = null,
            modifier = Modifier
                .fillMaxHeight()
                .fillMaxWidth(),
                contentScale = ContentScale.Crop
            )
            BirthdayGreetingWithText(message=message,from=from)
        }
    
    }
    
    @Preview(showBackground = true)
    @Composable
    fun BirthdayCardTPreview() {
        BirthdayCardTheme {
            BirthdayGreetingWithImage(message = "Happy Birthday hyeon!","- from zu")
        }
    }
    728x90