如何开启数据绑定?

How do I turn on data binding?

我看过this code in one of Google's Android examples。此处的代码示例:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="android.view.View" />

        <variable
            name="stats"
            type="com.example.android.architecture.blueprints.todoapp.statistics.StatisticsViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            <!-- look at this -->
            android:text="@{stats.status}"
            android:visibility="@{stats.showStatus ? View.VISIBLE : View.GONE}" />
    </LinearLayout>
</layout>

我想在我自己的项目中使用类似的东西,但它没有编译。我想它需要一些库或其他东西,但我找不到它。有什么建议吗?

在您的 build.gradle

中启用 dataBinding
android{
    ...
    ...
    defaultConfig{
        ...
        ...
        dataBinding{
            enabled true
        }
    }
}