kotlin - 我正在尝试使用 Jetpack Compose 在 Android 中设置背景图片,但图片无法填满整个页面

标签 kotlin background android-jetpack-compose appbar scaffold

  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/

相关文章:

android - kotlin中的lazy和lazyFast有什么区别?

html - 在一些背景图像上使用 css3 的降雪效果

android - Jetpack 撰写滚动监听器

android - 如何在可组合的屏幕中使用协同程序?

Android Compose - 平板电脑上的应用程序在使用 LazyColumn 时崩溃

android - 如何使用 Kotlin 设置 OnEditorActionListener

android - Droid 3 上的 ListView 背景为灰色

android - Jetpack Compose - 如何获取 LazyVerticalGrid 项目行索引?

java - 为什么我的 JGit checkout 返回 null ref?

javascript - Jquery 背景图像淡入淡出 - 有更好的方法吗?