JetpackCompose预览未显示
我似乎在 compose 中使用 Preview 时遇到问题,当我使用 @preview 注释 compose 方法时,布局面板没有出现。我假设我缺少一个依赖项,但我已经从这里复制并粘贴了代码https://developer.android.com/jetpack/compose/setup。有什么建议?(尝试了通常的清除缓存,重新打开项目等):)
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.0-alpha10'
kotlinCompilerVersion '1.4.21'
}
}
dependencies {
implementation 'androidx.compose.ui:ui:1.0.0-alpha10'
// Tooling support (Previews, etc.)
implementation 'androidx.compose.ui:ui-tooling:1.0.0-alpha10'
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation 'androidx.compose.foundation:foundation:1.0.0-alpha10'
// Material Design
implementation 'androidx.compose.material:material:1.0.0-alpha10'
// Material design icons
implementation 'androidx.compose.material:material-icons-core:1.0.0-alpha10'
implementation 'androidx.compose.material:material-icons-extended:1.0.0-alpha10'
// Integration with observables
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-alpha10'
implementation 'androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha10'
// UI Tests
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.0-alpha10'
implementation 'com.google.android.material:material:1.2.1'
}
这是我尝试使用预览(在 AS 中它说从未使用过函数“DefaultPreview”)
import androidx.compose.ui.tooling.preview.Preview
.....
@Preview
@Composable
fun DefaultPreview() {
Text(text = "Hello!")
}
回答
如果其他人遇到和我一样的问题(这是用户错误,但我也认为文档可能会更清楚),我将保留这一点。
Android Canary 有两个版本,beta 版和 arctic fox (alpha)。如果您想使用最新版本的 compose 库,请确保您使用的是 arctic fox。我发现 compose 库'androidx.compose.ui:ui-tooling:1.0.0-alpha08'(和更高版本)在 Canary 的 beta 版本中不能很好地工作。