【发布时间】:2019-08-02 17:31:20
【问题描述】:
我正在尝试从 main_activity.xml 设置 customView 的变量(buttonText),但出现此错误:
属性 buttonText (aka 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>
在这个post我发现:
在您的自定义视图中,按照您通常的方式扩展布局,并且 为您要设置的属性提供一个 setter:
我的 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 就不可能?
【问题讨论】:
标签: android android-databinding