kotlin
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
...
}
- 在 Android 开发中,使用 Compose 时,使用 ConstraintLayout,但是引入的是如下 ConstraintLayout
kotlin
import androidx.constraintlayout.widget.ConstraintLayout
问题原因
- 使用的是旧的 ConstraintLayout,对于 Compose,应该使用 constraintlayout-compose
处理策略
- 在 libs.versions.toml 文件中,定义版本号,定义具体的依赖库
xml
[versions]
...
constraintlayout-compose = "1.0.1"
xml
[libraries]
...
androidx-constraintlayout-compose = { group = "androidx.constraintlayout", name = "constraintlayout-compose", version.ref = "constraintlayout-compose" }
- 在模块级 build.gradle.kts 文件中,引用定义好的依赖
kotlin
dependencies {
...
implementation(libs.androidx.constraintlayout.compose)
}
- 使用 ConstraintLayout-compose 时
kotlin
import androidx.constraintlayout.compose.ConstraintLayout
kotlin
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
...
}