Android 应用程序看不到背景图片

问题描述 投票:0回答:1

使用 Jetpack-Compose 对 Kotlin 代码进行 USB 调试后,我在 Android 应用程序上看不到任何背景图像。 源码如下:

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))
    }
}

背景图片位于app > res > drawable > androidparty.png(nodpi)

我通过USB调试并在Android手机上运行了该应用程序(已安装),但背景不可见,但其余部分工作正常。如何获取应用程序的背景图片?

android kotlin background-image
1个回答
0
投票

在 MainActivity 类中,您正在调用 GreetingText 可组合项而不是 GreetingImage。即在您的 MainActivity 类中进行更改:

GreetingText(......)

GreetingImage(......)

应该可以解决这个问题

© www.soinside.com 2019 - 2024. All rights reserved.