1

我是 Android Jetpack Compose 的新手,尝试使用 Column 创建按钮列表,但应用程序因抛出错误而崩溃

无法启动活动 ComponentInfo{com.sample.composeUI/com.sample.composeUI.ui.homeScreen.HomeScreenActivity}:java.lang.IllegalStateException:未找到颜色!

代码:

@Composable
fun homeScreenCompose() {

    Column(
        crossAxisAlignment = CrossAxisAlignment.Center,
        mainAxisAlignment = MainAxisAlignment.Center,
        modifier = Spacing(16.dp)
    ) {
        Button(
            text = "ListView", onClick = {
            }, style = ContainedButtonStyle(
                color = Color.White,
                shape = RectangleShape,
                rippleColor = Color.DarkGray,
                elevation = Dp(4f)
            ))
    }
}

无法找到此问题的路径原因,我们将不胜感激。

4

1 回答 1

1

Android compose 在内部使用 Material design 为您的视图提供颜色和排版。所以你需要把你的函数包装在MaterialTheme composable function.

@Composable
fun homeScreenCompose() {
    MaterialTheme {
        Column(
            crossAxisAlignment = CrossAxisAlignment.Center,
            mainAxisAlignment = MainAxisAlignment.Center,
            modifier = Spacing(16.dp)
        ) {
            Button(
                text = "ListView", onClick = {
                }, style = ContainedButtonStyle(
                    color = Color.White,
                    shape = RectangleShape,
                    rippleColor = Color.DarkGray,
                    elevation = Dp(4f)
                )
            )
        }
    }
}

注意:当您使用 compose0.1.0-dev02版本时会发生此错误情况。版本不需要0.1.0-dev03

于 2020-01-17T17:43:37.020 回答