我试图创建一个可重复使用的布局included.xml
然后可以注入其他布局和通过标签属性进行自定义。下面是我有:Android数据绑定:无法注入属性到“包含”标签
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
以下错误:
Error:(17) No resource identifier found for attribute 'src123' in package 'com.myself.fancyapp'
没有任何人有一个想法,为什么出现这种情况,如何解决这一问题?谢谢。
'的xmlns:程序= “http://schemas.android.com/apk/res-auto”'记住这parent.xml,看到了输出 –
@RaviRupareliya感谢您的建议,但是,如前所述在我的文章的最后部分,它会引发编译时错误。 – stillwaiting
要确认您是否在使用parent.xml的类中实现DataBinding?或者你只是使用'setContentView'或'inflater'作为parent.xml文件? –