【问题标题】:Data binding does not support include as a direct child of a merge element数据绑定不支持包含作为合并元素的直接子元素
【发布时间】:2018-01-15 07:47:31
【问题描述】:

我正在尝试对现有项目使用数据绑定。作为其中的一部分,最初我试图摆脱所有 findViewById() 方法。

现在的问题是,我的布局如下:-

<merge >
<include
    android:id="@+id/my_login_process_view"
    layout="@layout/content_my_message_view"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/>
</merge>

将android绑定添加到此布局(将布局添加为父标记)后,它会抛出如下错误。

数据绑定不支持包含作为合并元素的直接子元素

我已经按照android官方指南Android data binding

我只是想摆脱上述布局文件的 findViewById。

任何建议将不胜感激。谢谢

【问题讨论】:

  • 你想合并什么?我只能在合并中看到一个布局。
  • 'content_my_message_view.xml' 是具有一些文本和图像视图的线性布局文件,我也在其他布局中使用此布局。所以,我做了一个单独的布局并使用了包含。 标签是我动态替换视图,所以我使用了合并标签。总而言之,merge 标记用于在运行时将在此处膨胀的视图。所以,在合并中我使用了包含标签,这样我就可以在运行时加载 content_my_message_view.xml。
  • 顾名思义,在实践中你需要不止一个来合并某事
  • 也不支持 w.r.t. 合并。到官方文档。看我的回答

标签: android android-databinding


【解决方案1】:

您提供的链接显然表示不支持。

数据绑定不支持包含作为合并的直接子项 元素。例如,不支持以下布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <merge>
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </merge>
</layout>

根据官方文档,它的工作原理是以下代码

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </LinearLayout>
</layout>

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多