[英]Android data binding attribute not found
我正在嘗試復制這個答案: 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>
我將它包含在另一個文件中:
<?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>
但是,該項目在清理和重建后拒絕編譯並出現錯誤:
AAPT: error: attribute hint123 (aka inc.company.appname:hint123) not found.
有任何想法嗎?
我也有
dataBinding {
enabled = true
}
在應用級別啟用build.gradle
我想我已經找到了解決方案。 要激活數據綁定,您需要使用@{}
表達式,並且大括號中的內容必須是有效的Java 代碼。 因此,文字字符串必須用引號括起來……必須將其編碼為"
在 XML 屬性值中。 把它們放在一起,你會得到:
<include
layout="@layout/custom_edit_text"
app:hint123="@{"Email"}"/>
數據綁定確實適用於包含文件,只是文字的語法有點復雜。 我有同樣的問題,這個表格在我的項目中有效。
使用@{ }
綁定變量
</layout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".simple.MainActivity">
<include layout="@layout/content_main_data_binding"
bind:name="@{ "Hello World" }" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
</layout>
<data>
<variable
name="name"
type="String" />
</data>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{ name }"/>
</layout>
GL
問題出在附帶的布局中。 您不能在其中設置屬性hint123。
另外,請注意,布局標簽的直接子級不支持include。
如下更新您包含的XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/custom_edit_text"
layout="@layout/custom_edit_text" />
</android.support.constraint.ConstraintLayout>
</layout>
要使用數據綁定設置提示,您必須在java或kotlin文件中進行設置。
這是Java代碼:
public class YourActivity extends AppCompatActivity {
YourActivityBinding mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.your_activity);
mBinding.customEditText.setHint123("Email");
mBinding.customEditText.executePendingBindings();
}
}
我正在使用Android 3.1.1。 以下代碼對我有用,如果可以使用它,則可以根據需要重用“提示”。 我的布局文件(custom_edit_text.xml)稍有更改,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<data>
<variable name="cName" type="String" />
<variable name="user" type="your.package.name.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.email}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.mobile}" />
<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:layout_width="match_parent"
android:layout_height="50dp"
android:hint="@{cName.toString()}"
android:paddingBottom="16dp"
android:paddingTop="14dp"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
第二個“類型”上方是程序包名稱+用戶類名稱。
我在一個單獨的文件中創建“ User”類,如下所示。
public class User {
String email;
String mobile;
User(String email, String mobile) {
this.email = email;
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public String getMobile() {
return mobile;
}
}
在MainActivity內部的onCreate()內部,我創建用戶對象,創建字符串並將其綁定。
String email = "xyz@yahoo";
String mobile = "9999";
User user = new User(email,mobile);
CustomEditTextBinding binding = DataBindingUtil.setContentView(this,R.layout.custom_edit_text) ;
binding.setCName("Yam May");
binding.setUser(user);
我像您一樣在應用程序級別build.gradle中啟用了綁定。
有關數據綁定的非常詳細的描述可以在https://www.vogella.com/tutorials/AndroidDatabinding/article.html中找到
以防萬一有人像我一樣遺漏了一個細節:您必須將父布局內容和包含的布局內容包含在 "" 和 ""中。 如果沒有該綁定,則不會在布局中使用。 當我沒有用這些標簽包含我的父布局時,我遇到了同樣的問題。
@big_m 提供的引用轉義解決方案有 2 個替代方案。
您可以將整個表達式用單引號引起來並在字符串周圍使用雙引號:
<include
layout="@layout/custom_edit_text"
app:hint123='@{"Email"}'/>
或者您可以在字符串周圍使用反引號:
<include
layout="@layout/custom_edit_text"
app:hint123="@{`Email`}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.