繁体   English   中英

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

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

我试图创建一个可重用的布局included.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

<?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

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'
}

结果,我尝试插入的按钮根本不包含任何图像。

如果在parent.xml我将xmlns:app更改为res-auto ,则在app/build/intermediate/data-binding-layout-out/debug/layout/parent.xml出现以下错误:

错误:(17)在包'com.myself.fancyapp'中找不到属性'src123'的资源标识符

有谁知道为什么会这样以及如何解决这个问题? 谢谢。

问题是您没有对变量使用绑定语法:

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

应该:

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

无关,但我认为不允许将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