Привязка данных Android - "Идентификатор ресурса не найден для атрибута"
Мой файл макета:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
app:fontName="Roboto-Regular.ttf"
android:layout_height="wrap_content"/>
</RelativeLayout>
Мой метод привязки привязки:
public class FontBinding {
@BindingAdapter("bind:fontName")
public static void setFontName(TextView view, @NonNull String fontName) {
String fontPath = "/fonts/" + fontName;
Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), fontPath);
view.setTypeface(typeface);
}
}
Ошибка, которую я получаю:
Error:(8) No resource identifier found for attribute 'fontName' in package 'com.example.databindingproject'
Следуйте руководству https://developer.android.com/tools/data-binding/guide.html.
Любые идеи о том, что я могу делать неправильно?
Ответы
Ответ 1
Вы должны использовать синтаксис привязки данных. Это должно быть:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
app:fontName='@{"Roboto-Regular.ttf"}'
android:layout_height="wrap_content"/>
Ответ 2
Эта же ошибка может также произойти, если вы забудете закрытие фигурной скобки:
android:text="@{viewModel.foo"