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

Android ์•ฑ์— ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ [ ๋„์ „๊ณผ์ œ ]

by hyeonha 2022. 11. 16.

๋ชฉ์ฐจ

    stringResource

     

    stringResource ๋ž‘ getString

    ์ €๋ ‡๊ฒŒ ํ•˜๋ฉด

    HappyBirthdayTheme {
        // A surface container using the 'background' color from the theme
        Surface(color = MaterialTheme.colors.background){
           BirthdayGreetingWithImage(stringResource(R.string.happy_birthday_text), from = getString(
                                  R.string.signature_text))
    }
    }

     

    getString( )์œผ๋กœ ๋‚˜์˜ค๋Š”๋ฐ ์™œ์ง€?

    HappyBirthdayTheme {
        // A surface container using the 'background' color from the theme
        Surface(color = MaterialTheme.colors.background){
           BirthdayGreetingWithImage(getString(R.string.happy_birthday_text),getString(R.string.signature_Text))
    }
    }

     

    stringResouce ๋ผ๊ณ  ์•ˆ๋œจ๊ณ  getString์œผ๋กœ ๋œจ๋Š”๋ฐ....?

    res/values/strings.xml ๋‚ด์šฉ

    <resources>
        <string name="app_name">Happy Birthday</string>
        <string name="happy_birthday_text">Happy birthday Sam!</string>
        <string name="signature_Text">-From Emma</string>
    </resources>

    1. ํ•จ์ˆ˜๋กœ ์Šคํฌ๋กคํ•˜๋ผ๊ณ  ํ•˜๋Š”๊ฒƒ์€ ๊ทธ๋ƒฅ ์ € ํ•จ์ˆ˜๋กœ ์ด๋™ํ•˜๋ผ๋Š” ๋ง์ธ๊ฐ€์š”?

    2. getString() , stringResource() ์ฐจ์ด

     

    ์‹ค์Šต ์†”๋ฃจ์…˜ ์ฝ”๋“œ

    HappyBirthdayTheme {
       // A surface container using the 'background' color from the theme
       Surface(color = MaterialTheme.colors.background) {
           BirthdayGreetingWithImage( stringResource(R.string.happy_birthday_text), "- from Emma")
       }
    }

    ๋‚ด ์‹ค์Šต ๊ฒฐ๊ณผ

    HappyBirthdayTheme {
        // A surface container using the 'background' color from the theme
        Surface(color = MaterialTheme.colors.background){
           BirthdayGreetingWithImage(getString(R.string.happy_birthday_text),"-From Emma")
    }
    }

    ์ „์ฒด ์†”๋ฃจ์…˜ ์ฝ”๋“œ

    package com.example.happybirthday
       
      import android.os.Bundle
      import androidx.activity.ComponentActivity
      import androidx.activity.compose.setContent
      import androidx.compose.foundation.Image
      import androidx.compose.foundation.layout.Box
      import androidx.compose.foundation.layout.Column
      import androidx.compose.foundation.layout.fillMaxHeight
      import androidx.compose.foundation.layout.fillMaxWidth
      import androidx.compose.foundation.layout.padding
      import androidx.compose.foundation.layout.wrapContentWidth
      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.happybirthday.ui.theme.HappyBirthdayTheme
       
      class MainActivity : ComponentActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContent {
      HappyBirthdayTheme {
      Surface(color = MaterialTheme.colors.background) {
      BirthdayGreetingWithImage( stringResource(R.string.happy_birthday_text),
      stringResource(R.string.signature_text))
      }
      }
      }
      }
      }
       
      @Composable
      fun BirthdayGreetingWithText(message: String, from: String) {
      // Create a column so that texts don't overlap
      Column {
      // Add two text, let them appear on top of each other
      Text(
      text = message,
      fontSize = 36.sp,
      modifier = Modifier
      .fillMaxWidth()
      .wrapContentWidth(Alignment.CenterHorizontally)
      .padding(start = 16.dp, top = 16.dp)
       
      )
      Text(
      text = from,
      fontSize = 24.sp,
      modifier = Modifier
      .fillMaxWidth()
      .wrapContentWidth(Alignment.CenterHorizontally)
      .padding(start = 16.dp, end = 16.dp)
      )
      }
      }
       
      @Composable
      fun BirthdayGreetingWithImage(message: String, from: String) {
      val image = painterResource(R.drawable.androidparty)
      // Create a box to overlap image and texts
      Box {
      Image(
      painter = image,
      contentDescription = null,
      modifier = Modifier
      .fillMaxHeight()
      .fillMaxWidth(),
      contentScale = ContentScale.Crop
      )
      BirthdayGreetingWithText(message = message, from = from)
      }
      }
       
      @Preview(showBackground = false)
      @Composable
      private fun BirthdayCardPreview() {
      HappyBirthdayTheme {
      BirthdayGreetingWithImage( stringResource(R.string.happy_birthday_text),
      stringResource(R.string.signature_text))
      }
      }

     

     

    728x90