Android 如何使用多个限定符绑定布局

Android 如何使用多个限定符绑定布局,android,data-binding,android-databinding,Android,Data Binding,Android Databinding,我有两个布局:一个用于v19+版本,另一个用于早期版本。它们包含具有不同ID的不同视图。我怎么说Android数据绑定框架我想同时使用这两种布局?它仅为一个布局生成视图(随机选择) 布局/temp_view.xml: <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools

我有两个布局:一个用于v19+版本,另一个用于早期版本。它们包含具有不同ID的不同视图。我怎么说Android数据绑定框架我想同时使用这两种布局?它仅为一个布局生成视图(随机选择)

布局/temp_view.xml:

<layout 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">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">

        <ImageView
            android:id="@+id/provider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="@dimen/size5"
            android:src="@{ProviderTypes.fromString(block.provider).getResId()}" />
    </FrameLayout>
</layout>
<layout 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">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">
        <ru.temp.utils.EmbedView
            android:id="@+id/media_embed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ru.temp.structure.static_material.CreditsView
            android:id="@+id/credits_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</layout>

layout-v19/temp_view.xml:

<layout 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">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">

        <ImageView
            android:id="@+id/provider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="@dimen/size5"
            android:src="@{ProviderTypes.fromString(block.provider).getResId()}" />
    </FrameLayout>
</layout>
<layout 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">

    <data>

        <variable
            name="block"
            type="ru.temp.model.content.blocks.WebMediaBlock" />

        <import type="ru.temp.model.Types.ProviderTypes" />

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:background="@android:color/white">
        <ru.temp.utils.EmbedView
            android:id="@+id/media_embed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ru.temp.structure.static_material.CreditsView
            android:id="@+id/credits_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</layout>

更新:


我终于找到了问题的根源。我不知道为什么,但当使用MinsdkVersion21时,它不会生成*BindingImpl文件。如果指定早期版本,它的工作原理是@yigit

数据绑定将生成一个基类,作为这两个变量的公共接口。当您调用
DataBindingUtil.setContentView(activity,R.layout.tmp_view
)时,数据绑定将负责创建正确的实例

例如,在您的示例中,它将生成

TempViewBinding
TempViewBindingImpl
TempViewBindingV19Impl
。 编译后,您可以在
/build/intermediates/classes//databinding/
中检查这些类

TempViewBinding
是基类,它将为每个布局组合变量和视图。
如果您没有在IDE中看到它们,则可能是AS中的自动完成错误(请提交错误)

不幸的是,它没有生成这样的Impl类。我在官方文件上找不到你提供的信息。无法确定我做错了什么如果你可以上传一个示例项目,我很高兴看到它的回复很晚,但它有很多时间来检测问题。请检查头部的更新。看看当前版本中的测试项目(主分支),数据绑定不会生成Impl文件。但是,如果您在build.gradle中设置minsdkversion15,就可以了!您正在使用android-apt。请尝试在那里报告。您也可以查看教程以供参考。它不包含问题的详细信息