找不到Android数据绑定属性 [英] Android data binding attribute not found
问题描述
我正在尝试复制以下答案:设置所包含布局的子元素的属性
I am trying to replicate this answer: Setting attribute of child element of included layout
我有一个简单的custom_edit_text.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="hint123" type="String" />
</data>
<android.support.design.widget.TextInputLayout
android:id="@+id/emailInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/emailField"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="14dp"
android:hint="@{hint123}"
android:textCursorDrawable="@null"
android:background="@drawable/edit_text_background"
android:fontFamily="@font/eina_regular"
android:textColor="@color/edit_text_color"
android:textColorHint="@color/edit_text_color"
android:textSize="15sp"
/>
</android.support.design.widget.TextInputLayout>
</layout>
我将它包含在另一个文件中
And I include it in another file:
<?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">
<include
layout="@layout/custom_edit_text"
app:hint123="Email"/>
</layout>
但是,该项目在经过清理后拒绝编译.重建并显示错误:
However the project refuses to compile after a clean & rebuild with the error:
AAPT: error: attribute hint123 (aka inc.company.appname:hint123) not found.
有什么想法吗?
我也有
dataBinding {
enabled = true
}
已在应用级别build.gradle
推荐答案
我认为我已经找到了解决方案.要激活数据绑定,您需要使用@{}
表达式,并且括号中的内容必须是有效的Java代码.因此,文字字符串必须用引号引起来......必须在XML属性值内编码为"
.全部放在一起,您将得到:
I think I've hit upon the solution. To activate data binding, you need to use a @{}
expression, and what's in the braces must be valid Java code. So a literal string must be enclosed in quotes... which must be encoded as "
inside an XML attribute value. Put it all together and you get:
<include
layout="@layout/custom_edit_text"
app:hint123="@{"Email"}"/>
数据绑定确实适用于包含文件,只是文字的语法有些复杂.我遇到了同样的问题,该表格正在我的项目中运行.
Data binding does work for include files, it's just that the syntax for a literal is a bit convoluted. I had the same issue and this form is working in my project.
这篇关于找不到Android数据绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!