実現したいこと
テキストの記述を1回にしたい
発生している問題・分からないこと
Developer.Androidというサイトのトレーニングコースを受講中です。
テキストと画像を表示する簡単なアプリを作るという課題で、以下のソースコードが回答例として出されました。
疑問点は、テキストを記述するコードが2回登場する点です。
stringResource(R.string.happy_birthday_text),
stringResource(R.string.signature_text)
が
上部のclass MainActivity~のところと
下部のprivate fun BirthdayCardPreview~のところの2回登場します。
何故同じことを2回も記述しているのでしょうか。
また、1度の記述で済ませるにはどのようなコードにすればよいのでしょうか。
該当のソースコード
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.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.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.text.style.TextAlign 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 { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { GreetingImage( stringResource(R.string.happy_birthday_text), stringResource(R.string.signature_text) ) } } } } } @Composable fun GreetingText(message: String, from: String, modifier: Modifier = Modifier) { // Create a column so that texts don't overlap Column( verticalArrangement = Arrangement.Center, modifier = modifier ) { Text( text = message, fontSize = 100.sp, lineHeight = 116.sp, textAlign = TextAlign.Center, modifier = Modifier.padding(top = 16.dp) ) Text( text = from, fontSize = 36.sp, modifier = Modifier .padding(top = 16.dp) .padding(end = 16.dp) .align(alignment = Alignment.End) ) } } @Composable fun GreetingImage(message: String, from: String, modifier: Modifier = Modifier) { // Create a box to overlap image and texts Box(modifier) { Image( painter = painterResource(id = R.drawable.androidparty), contentDescription = null, contentScale = ContentScale.Crop, alpha = 0.5F ) GreetingText( message = message, from = from, modifier = Modifier .fillMaxSize() .padding(8.dp) ) } } @Preview(showBackground = false) @Composable private fun BirthdayCardPreview() { HappyBirthdayTheme { GreetingImage( stringResource(R.string.happy_birthday_text), stringResource(R.string.signature_text) ) } }
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
片方の記述のみ削除してみると、エラーがでたりテキストが表示されなくなったりしました。
補足
プログラミング初学者です。回答について理解できなかったらすみません。
回答2件
あなたの回答
tips
プレビュー