kotlin - 我正在尝试使用 Jetpack Compose 在 Android 中设置背景图片

  1. 我想将图像填充到页面的最大尺寸并填充应用栏下方的边缘。
  2. 我可以在不使用脚手架的情况下将图像填充为完整背景,但在这种情况下我需要使用脚手架。
  3. 截图附上问题以便更好理解
  4. 你可以点击链接查看 enter image description here
@Composable
fun ScaffoldBackground() {

    Scaffold(
        topBar = {
            TopAppBar(
                modifier = Modifier
                    .fillMaxHeight(0.2f)
                    .clip(
                        shape = RoundedCornerShape(bottomEnd = 30.dp, bottomStart = 30.dp)
                    ),

                // Provide Title
                title = {
                    Text(
                        text = "Dashboard",
                    )

                }
            )
        },
    ) {

        Box(
            modifier = Modifier
                .fillMaxSize()
        ) {
            Image(
                modifier = Modifier
                    .fillMaxSize(), 
                painter = painterResource(R.drawable.ic_launcher_background),
                contentDescription = "background_image",
                contentScale = ContentScale.FillBounds
            )


        }

    }
}

the image cant fill the the edges of app bar

最佳答案

我试过这段代码,它工作正常。这里重要的是确保您放入 Scaffold 中的内容应该有一些透明区域,否则背景图像将不可见。

Box {
    Image(
        modifier = Modifier.fillMaxSize(),
        painter = painterResource(R.drawable.ic_launcher_background),
        contentDescription = "background_image",
        contentScale = ContentScale.FillBounds
    )
    Scaffold(
        backgroundColor = Color.Transparent,   // Make the background transparent
        topBar = {
            TopAppBar(
                modifier = Modifier
                    .fillMaxHeight(0.2f)
                    .clip(
                        shape = RoundedCornerShape(
                            bottomEnd = 30.dp,
                            bottomStart = 30.dp
                        )
                    ),
                title = {
                    Text(text = "Dashboard")
                }
            )
        },
    ) {
        // Scaffold content
    }
}

关于kotlin - 我正在尝试使用 Jetpack Compose 在 Android 中设置背景图片,但图片无法填满整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70064992/

相关文章:

javascript - 为什么不重新分配 forEach 中的参数元素起作用

angular - 我需要取消订阅 Angular Observable 吗?

c++ - 多线程段故障析构函数

python - 返回 boolean 数组中 "false"值的索引

scala - Scala 3 中的递归高阶函数类型

angular - "declare global{ interface Window{ analy

python - 在 Numpy 或 Pandas 中的每一行之后插入相似行(只有一列发生变化)的有

python - 如何从带日期的字符串列表中过滤掉字符串?

typescript - 如何防止将 Prop 传递给内部样式组件

angular - 类型错误 : Cannot read properties of undefin