问题描述
使用数据绑定时,我无法按照数据绑定指南
While using data binding , I am not able to get class MainActivityBinding
as per Data Binding Guide
我的布局名称是activity_main.xml
.我还看到 Android-DataBinding-如何和何时会生成Binding类?但这对我没有帮助.
My layout name is activity_main.xml
.I am also see Android - DataBinding - How and when the Binding classes will be generated? but it can't help me.
推荐答案
感谢大家的回答.我找到了使用ContentMainBinding
类名称进行数据绑定的解决方案.让我解释一下.
Thanks to all for your answer.I found solution with ContentMainBinding
class name for data binding.Lets me explain.
注意::将布局与 <include ...
一起使用时,这是具有数据绑定功能的<include layout="@layout/content_main"
,与包含布局名称有关的类名称.这是ContentMainBinding
NOTE: While using layout with <include ...
here is <include layout="@layout/content_main"
having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding
我的布局文件如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.databindingdemo.app.MainActivity">
...
<include layout="@layout/content_main" />
...
</android.support.design.widget.CoordinatorLayout>
content_main.xml 是我在其中添加了数据绑定布局代码的布局.
And content_main.xml is layout where I added my Data Binding layout code.
因此可以使用 ContentMainBinding
So instead of using MainActivityBinding
it can resolved with ContentMainBinding
对我有用的代码如下:
//Code for data binding
ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
user = new User("Pranay", "Patel", "[email protected]", "9999999999");
contentMainBinding.setUser(user);
完成.
这篇关于无法解析Android数据绑定类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!