我无法在 Jetpack Compose 版本 1.0.5 中预览带有数据的 ViewDataBinding
我设法使用:
片段.kt
@Preview
@Composable
fun PreviewExample() {
AndroidViewBinding(MyViewBinding::inflate)
}
Run Code Online (Sandbox Code Playgroud)
my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
片段.kt
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
my_view_with_layout_tags.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> …Run Code Online (Sandbox Code Playgroud) 我已经尝试过使缓存无效。清理构建和重建项目也已完成。但我仍然不断收到以下未解决的参考:Dispatchers、launch、withContext、delay...但是 CoroutineScope没有标记为未解决的参考,即import kotlinx.coroutines。* 未标记为未知,并且该项目正在编译(它已经工作了几个月,它不是最近的项目)。
安卓工作室:4.0.1
设置 > 语言和框架 > Kotlin :当前kotlin 插件版本:1.4.10-release-Studio4.0-1
构建.gradle
buildscript {
ext {
// KOTLIN
kotlin_coroutines_version = '1.3.5'
kotlin_version = '1.3.71'
...
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Run Code Online (Sandbox Code Playgroud)
模块构建.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release { …Run Code Online (Sandbox Code Playgroud) android kotlin android-lint android-studio kotlin-coroutines