1

当我尝试使用 jetpack 的 compose 库时,我被卡在了 jetpack 文档中提到的步骤(来源:https ://developer.android.com/jetpack/compose/setup#compose-compiler )。由于出现以下异常,我无法使用 kotlinPlugin。

Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method kotlinPlugin() for arguments [androidx.compose:compose-compiler:0.1.0-dev02] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

此外,我无法使用darkThemeColorslightThemeColors。以下是我添加的 Gradle 依赖项。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    kotlinPlugin "androidx.compose:compose-compiler:0.1.0-dev02"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.ui:ui-framework:0.1.0-dev02'
    implementation 'androidx.ui:ui-layout:0.1.0-dev02'
    implementation 'androidx.ui:ui-material:0.1.0-dev02'
    implementation 'androidx.ui:ui-tooling:0.1.0-dev02'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.50'
}

我是否缺少任何依赖项?我检查了MaterialThemeMaterialColors类,但没有找到 darkThemeColors 和 lightThemeColors。

4

2 回答 2

2

jetpackCompose示例应用程序jetnewsdarkThemeColors并在下面lightThemeColors的文件示例中定义,Themes.kt因此您需要定义颜色。

val lightThemeColors = MaterialColors(
    primary = Color(0xFFDD0D3C),
    primaryVariant = Color(0xFFC20029),
    onPrimary = Color.White,
    secondary = Color.White,
    onSecondary = Color.Black,
    background = Color.White,
    onBackground = Color.Black,
    surface = Color.White,
    onSurface = Color.Black,
    error = Color(0xFFD00036),
    onError = Color.White
)

/**
 * Note: Dark Theme support is not yet available, it will come in 2020. This is just an example of
 * using dark colors.
 */
val darkThemeColors = MaterialColors(
    primary = Color(0xFFEA6D7E),
    primaryVariant = Color(0xFFDD0D3E),
    onPrimary = Color.Black,
    secondary = Color(0xFF121212),
    onSecondary = Color.White,
    surface = Color(0xFF121212),
    background = Color(0xFF121212),
    onBackground = Color.White,
    onSurface = Color.White
) 

jetnews在下面的项目 截图中在此处输入图像描述 查看jetnews此链接https://developer.android.com/jetpack/compose/setup#sample

于 2019-10-25T06:11:34.483 回答
1

您可以根据需要定义darkThemeColorslightThemeColors

只需1.0.0-beta02使用lightColorsdarkColors功能。
就像是:

val purple500 = Color(0xFF6200EE)

private val LightColorPalette = lightColors(
        primary = purple500,
        primaryVariant = purple700,
        secondary = Color(0xFF03DAC6),
        onSecondary = Color.White
)

例如,您可以定义:

@Composable
fun MaterialThemeSample(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {

    val lightColorsPalette = lightColors(
            primary = Color(0xFF1EB980),
            secondary = Color(0xFF03DAC6)
    )
    val darkColorsPalette = darkColors(
            primary = Color(0xFF66ffc7),
            secondary = Color.Red
    )

    val colors = if (darkTheme) darkColorsPalette else lightColorsPalette

    MaterialTheme(
            colors = colors,
            typography = typography,
            shapes = shapes,
            content = content
    )
}

并使用它:

    setContent {
        MaterialThemeSample() {

            .....
        }
    }
于 2019-10-25T06:47:17.057 回答