ViewDataBinding Jetpack Compose 预览版中的 XML 文件

ViewDataBinding XML file in Jetpack Compose Preview

提问人:Xsims 提问时间:11/26/2021 更新时间:11/26/2021 访问量:1925

问:

我无法在 Jetpack Compose 版本 1.0.5 中使用数据预览 ViewDataBinding

我设法使用:

  1. 没有<布局的 XML 文件>标签,这要归功于: https://stackoverflow.com/a/67016873/13170425

片段.kt

@Preview
@Composable
fun PreviewExample() {
    AndroidViewBinding(MyViewBinding::inflate)
}

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>
  1. 具有<布局>标签但没有<数据>标签的 XML 文件,这要归功于: https://stackoverflow.com/a/65653754/13170425

片段.kt

@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
    })
}

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>

但是我不能对带有<布局>的 xml 文件做同样的事情,<数据>标签**

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

1.

@Preview
@Composable
fun PreviewExample() {
    AndroidView(factory = {
        val binding = ItemFabScanBinding.inflate(LayoutInflater.from(it))
        binding.scanButtonVisible = true
        binding.root
    })
}
@Preview
@Composable
fun PreviewExample() {
    AndroidViewBinding(ItemFabScanBinding::inflate)
}
@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
    })
}
@Preview
@Composable
fun PreviewExample() {
    AndroidView(factory = {
        val binding = DataBindingUtil.inflate<ItemFabScanBinding>(LayoutInflater.from(it),
            R.layout.item_fab_scan,
            null,
            false)
        binding.scanButtonVisible = true
        binding.root
    })
}

item_fab_scan.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="scanButtonVisible"
            type="Boolean" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginBottom="@dimen/spacing_8">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/bt_scan"
            style="@style/Dlc.ExtendedFloatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_8"
            android:elevation="@dimen/spacing_8"
            android:text="@string/scan"
            app:elevation="20dp"
            app:icon="@drawable/ic_scan_barcode_white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:visibleOrGone="@{scanButtonVisible}" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Android XML android-jetpack-compose android- 数据绑定

评论


答: 暂无答案