๋ชฉ์ฐจ
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
'2023 ์๋๋ก์ด๋ > ์๋๋ก์ด๋ ์คํฐ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฐ์ต: Compose ๊ธฐ๋ณธ ์ฌํญ (0) | 2022.11.17 |
---|---|
10. ์๋ฃจ์ ์ฝ๋ ๊ฐ์ ธ์ค๊ธฐ ~ 11. ๊ฒฐ๋ก (0) | 2022.11.16 |
Android ์ฑ์ ์ด๋ฏธ์ง ์ถ๊ฐ 6~ (0) | 2022.11.16 |
Android ์ฑ์ ์ด๋ฏธ์ง ์ถ๊ฐ ~5 (0) | 2022.11.16 |
๊ธฐ๋ณธ ๋ ์ด์์ ๋ง๋ค๊ธฐ (0) | 2022.11.16 |