Iho*_*hor 6 android android-jetpack-compose android-jetpack-compose-preview android-jetpack-compose-modifier
我开始将代码迁移到 Compose,但遇到了 @Preview 问题。例如,当我在modifier.fillMaxWidth()没有默认参数“fraction = 1f”的情况下使用时,我遇到渲染问题
java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.SizeKt.fillMaxWidth$default(androidx.compose.ui.Modifier, java.lang.Float, int, java.lang.Object) '
或使用不带底部参数的填充
.padding(
top = dimensionResource(id = R.dimen._4sdp),
start = dimensionResource(id = R.dimen._16sdp),
end = dimensionResource(id = R.dimen._16sdp)
)
Run Code Online (Sandbox Code Playgroud)
java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.PaddingKt.padding-qDBjuR0$default(androidx.compose.ui.Modifier, androidx.compose.ui.unit.Dp, androidx. compose.ui.unit.Dp、androidx.compose.ui.unit.Dp、androidx.compose.ui.unit.Dp、int、java.lang.Object)'
这是我使用的代码
@Composable
fun Info(
modifier: Modifier = Modifier,
step: String,
stepInfo: String,
) {
Column(modifier = modifier.fillMaxWidth(fraction = 1f)) {
Text(
text = step,
modifier = Modifier
.background(
color = colorResource(id = R.color.color_on_background_variant3),
shape = RoundedCornerShape(90)
)
.size(dimensionResource(id = R.dimen._28sdp))
.padding(dimensionResource(id = R.dimen._6sdp))
.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Center
)
Text(
text = stepInfo,
textAlign = TextAlign.Center,
modifier = Modifier
.padding(
top = dimensionResource(id = R.dimen._4sdp),
start = dimensionResource(id = R.dimen._16sdp),
end = dimensionResource(id = R.dimen._16sdp),
bottom = 0.dp
)
.align(Alignment.CenterHorizontally),
)
}
}
@Preview
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, name = "InfoPreviewNight")
@Composable
fun InfoPreview() {
MdcTheme {
Info(
step = "1",
stepInfo = ".............."
)
}
}
Run Code Online (Sandbox Code Playgroud)
buildld.gradle(:应用程序)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.firebase.appdistribution'
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.1'
}
dependencies {
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation "androidx.compose.runtime:runtime"
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.foundation:foundation"
implementation "androidx.compose.foundation:foundation-layout"
implementation "androidx.compose.material:material"
implementation "androidx.compose.runtime:runtime-livedata"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "com.google.accompanist:accompanist-themeadapter-material:0.30.1"
debugImplementation "androidx.compose.ui:ui-tooling"
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
}
Run Code Online (Sandbox Code Playgroud)
Gradle 插件版本 ------ 7.2.2 Gradle 版本 ------- 7.3.3
我尝试更改 kotlinCompilerExtensionVersion 但没有得到结果,并且stackoverflow 问题没有帮助。
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |