Android 数据绑定库包括don';没有变量就不能工作
由于George Mount从1.0-rc4开始,我们在使用数据绑定时不再需要include中的变量: buttons.xml:Android 数据绑定库包括don';没有变量就不能工作,android,android-databinding,Android,Android Databinding,由于George Mount从1.0-rc4开始,我们在使用数据绑定时不再需要include中的变量: buttons.xml: <layout xmlns:andr...> <Button android:id="@+id/button" ...." /> 我认为你误解了乔治在链接帖子上的回答;如果要像在包含的工具栏布局中一样使用@{toolbarViewModel.title}引用该变量,则仍然需要该变量 如果在build.gradle中启用
<layout xmlns:andr...>
<Button
android:id="@+id/button"
...." />
我认为你误解了乔治在链接帖子上的回答;如果要像在包含的工具栏布局中一样使用@{toolbarViewModel.title}
引用该变量,则仍然需要该变量
如果在build.gradle中启用了数据绑定,并将布局包装在一个额外的
标记中,您将获得一个自动生成的ViewDataBinding
类,该类包含对布局中具有ID的任何视图的解析引用(例如binding.toolbar
,或类似的内容)。这不会自动为您绑定数据,但允许您摆脱任何findViewById
调用
有关更多信息,请参阅。您是否使用最新版本的数据绑定库。。?实施工作已经完成。请同时发布相关的Java
code.:)您好,我使用2.0.0-beta4插件,并使用dataBinding{enabled=true}
打开绑定。所以我认为这是目前最新的@Amy,我认为这里不需要java,因为你只需要在第一个布局中传递工具栏标题作为ObservableField就可以复制它,你会看到相同的结果。对我来说也不管用是的,我认为你是对的。这是误解。只要再读一遍他的答案,现在我看到他只是在谈论id,从1.0-rc4中,不需要传递变量来获得id为binding.buttons.button1的视图,就像前面提到的版本一样。
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"/>
....
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class="LoginPhoneFragmentBinding">
<variable
name="toolbarViewModel"
type="ru.mobileup.myalarm2.binding.ToolbarViewModel"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar"
android:id="@+id/toolbarBinding"/>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ToolbarThemeOverlay"
app:navigationIcon="@{toolbarViewModel.navigationIconResId}"
app:navigationOnClickListener="@{toolbarViewModel.navigationOnClickListener}"
app:title="@{toolbarViewModel.title}"
app:menu="@{toolbarViewModel.menuResId}"
app:menuListener="@{toolbarViewModel.onMenuItemClickListener}"/>