简体   繁体   English

Android数据绑定:无法将属性注入“ include”标签

[英]Android data binding: cannot inject an attribute into “include” tag

I'm trying to create a reusable layout included.xml which then could be injected into other layouts and customized via tag attributes. 我试图创建一个可重用的布局included.xml然后可能被注入到其它的布局,并通过标记属性定制。 Here's what I have: 这是我所拥有的:

res/layout/parent.xml : res/layout/parent.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/com.myself.fancyapp"
    >

    <include layout="@layout/included"
            app:src123="@drawable/my_icon" />

</layout>

res/layout/included.xml : res/layout/included.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <data>
        <variable name="src123" type="android.graphics.drawable.Drawable" />
    </data>

    <android.support.design.widget.FloatingActionButton
        android:src="@{src123}" />
</layout>

app/build.gradle : app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dataBinding {
        enabled = true
    }
    ....
}

dependencies {
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

As an outcome, the button which I try to inject doesn't contain any image at all. 结果,我尝试插入的按钮根本不包含任何图像。

If in parent.xml I change xmlns:app to res-auto , I have the following error in app/build/intermediate/data-binding-layout-out/debug/layout/parent.xml : 如果在parent.xml我将xmlns:app更改为res-auto ,则在app/build/intermediate/data-binding-layout-out/debug/layout/parent.xml出现以下错误:

Error:(17) No resource identifier found for attribute 'src123' in package 'com.myself.fancyapp' 错误:(17)在包'com.myself.fancyapp'中找不到属性'src123'的资源标识符

Does anybody have an idea why this happens and how to fix this? 有谁知道为什么会这样以及如何解决这个问题? Thanks. 谢谢。

The problem is that you're not using binding syntax for the variable: 问题是您没有对变量使用绑定语法:

<include layout="@layout/included"
        app:src123="@drawable/my_icon" />

should be: 应该:

<include layout="@layout/included"
        app:src123="@{@drawable/my_icon}" />

Unrelated, but I don't think that includes are allowed as the root element of a layout. 无关,但我认为不允许将include作为布局的根元素。 I believe that you're going to have to have a normal View surrounding the include. 我相信您将必须对包含文件有一个普通的视图。

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/com.myself.fancyapp"
    >

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent">
        <include layout="@layout/included"
                app:src123="@{@drawable/my_icon}" />
    </FrameLayout>
</layout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM