正如 George Mount所说 ,从 1.0-rc4 开始,我们在使用数据绑定时不再需要 include 中的变量:
按钮.xml:
<layout xmlns:andr...>
<Button
android:id="@+id/button"
...." />
主.xml:
<layout xmlns:andr...
...
<include layout="@layout/buttons"
android:id="@+id/buttons"/>
....
但我试了一下,报错:
错误:(10, 31) 标识符必须具有来自 XML 文件的用户定义类型。工具栏视图模型缺少它
我有包含的工具栏:
<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}"/>
怎么了?
注意:我知道使用传递的变量一切正常。我试图弄清楚乔治提到的用途。