我在我的 Android 项目中使用数据绑定,并且我已经完成了所有必要的设置。但是,虽然我可以访问某些片段生成的绑定类(例如 FragmentTemplateBinding),但我无法访问其他片段的绑定类。
到目前为止我已尝试解决该问题:
验证数据绑定设置正确。 尝试在 Android Studio 中使缓存无效并重新启动。 确保在 build.gradle 中启用数据绑定。 清理并重建项目(清理项目和重建项目)。
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.example.MyViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- UI elements -->
</LinearLayout>
</layout>
android {
...
viewBinding {
enabled = true
}
dataBinding {
enabled = true
}
}
您应该在嵌套在 android 内部的 buildFeature 中拥有 viewBinding。
android {
...
buildFeatures {
viewBinding true
}
}
有关更多详细信息,您可以访问此官方文档链接