使用 Color.Green 在 Jetpack Compose Card 中设置背景颜色时出错

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

我在 Jetpack Compose 代码中遇到问题,将卡片的背景颜色设置为

Color.Green
会导致错误。这是相关代码的片段:

Card(
    modifier = Modifier
        .width(200.dp)
        .height(300.dp)
        .padding(12.dp),
    shape = RoundedCornerShape(corner = CornerSize(10.dp)),
    backgroundColor = Color.Green // error is showing in this code why
) {
    // Content inside the Card
}

Despite using the correct Color import (import androidx.compose.ui.graphics.Color), the compiler is flagging an error specifically on the backgroundColor = Color.Green line. I've ensured that the necessary dependencies are in place, and other colors from the Color class are working as expected.

Is there something specific about the Color.Green value that might be causing this error within the context of a Jetpack Compose Card? Any insights or suggestions for resolving this issue would be greatly appreciated. Thank you!
android android-jetpack
1个回答
0
投票

您确定导入了所有撰写元素吗?您共享的代码片段是正确的,我认为某些导入可能会导致问题。

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

@Composable
fun App() {
    Card(
        modifier = Modifier
            .width(200.dp)
            .height(300.dp)
            .padding(12.dp),
        shape = RoundedCornerShape(corner = CornerSize(10.dp)),
        backgroundColor = Color.Green,
    ) {}
}
© www.soinside.com 2019 - 2024. All rights reserved.