kotlin compose 中的“Color”属性错误

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

我刚刚使用 compose 开始了一个新项目。第一个教程步骤是按以下方式更改颜色。我收到错误并且找不到方法。只是多了一行代码,所以我觉得找到错误并不是那么困难。我就是找不到。

enter image description here

package com.example.kotlin_compose_tutorial

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.kotlin_compose_tutorial.ui.theme.KotlincomposetutorialTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            KotlincomposetutorialTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(
        text = "Hello $name!",
        color = Color.Blue
    )
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    KotlincomposetutorialTheme {
        Greeting("Android")
    }
}

这是页面上的完整代码。

我还从 Android 开发者页面复制并粘贴了此代码块,并得到了相同的错误。

@Composable
fun BlueText() {
    Text("Hello World", color = Color.Blue)
}
android kotlin attributes
2个回答
1
投票

看起来好像您还没有导入 Color 类。因此,参考“颜色”是未解决的。确保将其添加到您的导入中:

import androidx.compose.ui.graphics.Color

0
投票

问题是导入了错误的类。导入的默认值不适用于 Compose。本来应该是

import androidx.compose.ui.graphics.Color

我猜这是一个菜鸟错误。

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