根据http://developer.android.com/tools/data-binding/guide.html#imports,我们可以在可见性中使用这样简单的表达式:
<TextView
..
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在include标签中执行相同操作时,如下所示:
<include
android:id="@+id/image_layout"
layout="@layout/image_layout"
android:visibility="@{notification.notifType == 0 ? View.VISIBLE : View.GONE}"/>
Run Code Online (Sandbox Code Playgroud)
然后Studio不仅以红色显示表达式,而且在构建时它会在自动生成的绑定类中显示以下错误:
错误:(138,29)错误:找不到符号方法setVisibility(int)
这是自动生成的绑定类中发生错误的位置
// batch finished
if ((dirtyFlags & 0x3L) != 0) {
// api target 1
this.imageLayout.setVisibility(NotifTypeNotificatio1);
}
imageLayout.executePendingBindings();
Run Code Online (Sandbox Code Playgroud)