Jetpack Compose 预览中的 ViewDataBinding XML 文件

Xsi*_*ims 12 xml android android-databinding android-jetpack-compose

我无法在 Jetpack Compose 版本 1.0.5 中预览带有数据的 ViewDataBinding

我设法使用:

  1. 没有<layout>标签的Xml 文件感谢:/sf/answers/4691181141/

片段.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)
  1. 带有<layout>标签但没有<data>标签的 Xml 文件,感谢:/sf/answers/4595762811/

片段.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>
</layout>
Run Code Online (Sandbox Code Playgroud)

我不能对带有<layout><data>标签的xml 文件做同样的事情**

这是我到目前为止所尝试的:

1.

@Preview
@Composable
fun PreviewExample() {
    AndroidView(factory = {
         val view = LayoutInflater.from(it).inflate(R.layout.my_view_with_layout_tags, null, false)
         val textView = view.findViewById<TextView>(R.id.text_view)
         textView.text = "replacing text from Compose Function"
         view
    })
}
Run Code Online (Sandbox Code Playgroud)
<?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>
</layout>
Run Code Online (Sandbox Code Playgroud)
@Preview
@Composable
fun PreviewExample() {
    AndroidView(factory = {
        val binding = ItemFabScanBinding.inflate(LayoutInflater.from(it))
        binding.scanButtonVisible = true
        binding.root
    })
}
Run Code Online (Sandbox Code Playgroud)
@Preview
@Composable
fun PreviewExample() {
    AndroidViewBinding(ItemFabScanBinding::inflate)
}
Run Code Online (Sandbox Code Playgroud)

item_fab_scan.xml

@Preview
@Composable
fun PreviewExample() {
    AndroidView(factory = {
        val view = LayoutInflater.from(it).inflate(R.layout.item_fab_scan, null, false)
        val binding = DataBindingUtil.findBinding<ItemFabScanBinding>(view)
        binding?.scanButtonVisible = true
        binding?.root ?: view
    })
}
Run Code Online (Sandbox Code Playgroud)