android - 如何在 Android Jetpack Compose 中向 Card 对象添加图像背景?

标签 android android-layout android-jetpack-compose android-jetpack-compose-layout

我正在开发一个 Jetpack Compose 应用程序,该应用程序使用 LazyColumn 显示玩家列表,并且我正在尝试向每个 Card 添加图像背景(在代表玩家的可组合函数 ProfileCard)。

我使用 Box 对象来保存每个条目的内容,并声明一个 Image 对象作为其第一个元素。 根据我的理解, Box 对象中的所有元素都是“堆叠”的,以便稍后声明的元素(进一步向下)在视觉上显示在先前声明的元素(进一步向上)的顶部,所以我不确定为什么图像背景(首先声明)呈现在 Text 元素的顶部,该元素在 ProfileContent 可组合项的 Line#93 中进一步声明: enter image description here

这是禁用深色蜂窝图像元素的屏幕截图: enter image description here

在这里,启用了深色蜂窝图像元素: enter image description here

不知何故,背景图像正确地呈现在播放器图标的后面,但它却呈现在Text元素的顶部 - 尽管两者都是图标和文本在同一个 Row 对象中声明。 有人知道如何解决此问题并强制将背景图像渲染在两个元素后面吗?

最佳答案

它不会在 TextField 顶部呈现,它看起来在它的顶部,因为 TextField 的 背景与它后面的 Image ,所以看起来像是与它们重叠。 OP screenshot

我尝试实现您的代码并将 TextField backgroundColor 更改为 Color.LightGray

 Card(
        modifier = Modifier
            .fillMaxWidth()
            .size(90.dp)
            .padding(top = 6.dp)
            .wrapContentHeight(align = Alignment.Top),
        shape = CutCornerShape(topEnd = 20.dp),
        elevation = 8.dp
    ) {

        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {

            Image(
                painter = painterResource(id = R.drawable.honeycomb),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.matchParentSize()
            )

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // ProfilePicture() composable
                Box(
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape)
                        .background(Color.Red)
                )

                Spacer(modifier = Modifier.size(size = 16.dp))

                // ProfileContent() composable
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(end = 16.dp)
                ) {
                    TextField(
                        value = "Text",
                        onValueChange = {},
                        label = { Text("Player Name")},
                        colors = TextFieldDefaults.textFieldColors(
                            backgroundColor = Color.LightGray
                        )
                    )
                }
            }
        }
    }

White Bg With Black honey comb BG

编辑:将已发布的解决方案描述从 Text 重构并更正为 TextField

关于android - 如何在 Android Jetpack Compose 中向 Card 对象添加图像背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74022063/

相关文章:

xml - 如何在我的自定义形状 xml 中添加图像

Android - 以编程方式设置两个重力参数

android - Jetpack Compose collectAsState 问题

android-jetpack-compose - 如何正确从资源中获取Color(JetpackCompose)?

java - setSpeakerphoneOn 的奇怪 AudioManager 行为

java - Android TextView 线条背景

java - 回收 ImageView 的 Bitmap

android - 如何让 Android 应用程序在下载和安装后自动运行?

android - 将 TextView 包裹在 ImageView 周围

android - 如何使用箭头键将焦点从撰写按钮移动到撰写文本字段?