Я пытаюсь установить переменную (buttonText) customView из main_activity.xml, но я получил эту ошибку:
атрибут buttonText (он же com.example.testbinding: buttonText) не найден.ошибка: не удалось связать файловые ресурсы.
customView.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"
xmlns:tools="http://schemas.android.com/tools" >
<data>
<variable
name="buttonText"
type="String" />
</data>
<LinearLayout
android:id="@+id/container"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:text="@{buttonText}" />
</LinearLayout>
</layout>
main_activity.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.testbinding.CustomView
android:id="@+id/customView"
...
app:buttonText = "Test button text"
...
>
</com.example.testbinding.CustomView>
</android.support.constraint.ConstraintLayout>
</layout>
В этом сообщении Iнайдено:
В пользовательском представлении раздуте макет, как обычно, и предоставьте установщик для атрибута, который вы хотите установить:
Мой класс CustomView имеет общедоступный установщикдля свойства buttonText:
public class CustomView extends LinearLayout {
private CustomViewBinding binding;
private String buttonText;
public CustomView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
binding = DataBindingUtil.inflate(inflater, R.layout.custom_view, this, true);
}
public String getButtonText() {
return buttonText;
}
public void setButtonText(String buttonText) {
this.buttonText = buttonText;
}
}
Может кто-нибудь сказать мне, что с ним не так?Или это невозможно без BindingAdapter?