Исходный код выглядит следующим образом:
Код: Выделить всё
package com.example.happybirthday
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.res.painterResource
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
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
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
) {
GreetingText(message = stringResource(R.string.happy_birthday_text), from= getString(
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
.padding(8.dp)
.fillMaxSize()
) {
Text(
text = message, //This GreetingText() function displays text in the UI. It does so by calling the Text() composable function....predefined
fontSize = 97.sp,
lineHeight = 116.sp,
textAlign = TextAlign.Center
)
Text(
text = from,
fontSize = 36.sp,
modifier = Modifier
.padding(16.dp)
.align(alignment = Alignment.CenterHorizontally)
)
}
}
@Composable
fun GreetingImage(message: String , from: String, modifier: Modifier = Modifier) {
val image = painterResource(R.drawable.androidparty)
//create a box to overlap image and texts
Box {
Image(
painter = image,
contentDescription = null,
contentScale = ContentScale.Crop ,
alpha = 0.5F
)
GreetingText(
message = message,
from = from,
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
)
}
}
@Preview(
showBackground = true,
)
@Composable
private fun BirthdayCardPreview() {
HappyBirthdayTheme {
GreetingImage( stringResource(R.string.happy_birthday_text),
stringResource(R.string.signature_text))
}
}
Я выполнил отладку по USB и запустил приложение на телефоне Android. (установил), но фона не видно а в остальном работает нормально. Как получить фоновое изображение в приложении?
Подробнее здесь: https://stackoverflow.com/questions/778 ... ndroid-app
Мобильная версия