2016-12-14 60 views
0

我试图创建一个可重复使用的布局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:appres-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'

没有任何人有一个想法,为什么出现这种情况,如何解决这一问题?谢谢。

+0

'的xmlns:程序= “http://schemas.android.com/apk/res-auto”'记住这parent.xml,看到了输出 –

+0

@RaviRupareliya感谢您的建议,但是,如前所述在我的文章的最后部分,它会引发编译时错误。 – stillwaiting

+0

要确认您是否在使用parent.xml的类中实现DataBinding?或者你只是使用'setContentView'或'inflater'作为parent.xml文件? –

回答

2

的问题是,你不使用绑定语法变量:

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

应该是:

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

无关,但我不认为包括被允许作为布局的根元素。我相信你将不得不围绕包含一个正常的观点。

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

谢谢,乔治,这工作就像一个魅力! – stillwaiting

相关问题