Android CustomView 中的数据绑定 <merge> 无法解析资源
Android DataBinding in CustomView <merge> not able to resolve Resources
尝试在 CustomView(Group) 中使用 DataBinding
布局如下所示:
<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">
<merge
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/customview_checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="@string/checkbox_value_key"
android:text="My CheckBox"/>
....
</merge>
</layout>
CustomView(扩展 FrameLayout)inflation:
view = DataBindingUtil.inflate((LayoutInflater) getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE), R.layout.custom_view, this, true);
导致此错误:
Error:(46, 61) error: cannot find symbol method getResources()
生成的绑定Class:
public ViewBodyPartsBinding(android.databinding.DataBindingComponent bindingComponent, View[] root) {
super(bindingComponent, root[0], 0);
final Object[] bindings = mapBindings(bindingComponent, root, 9, sIncludes, sViewsWithIds);
this.mboundView0 = (android.widget.ImageView) bindings[0];
this.mboundView0.setTag(null);
this.mboundView2 = (android.support.percent.PercentRelativeLayout) bindings[2];
this.mboundView2.setTag(null);
....
this.customviewCheckbox1 = (android.widget.CheckBox) bindings[1];
this.customviewCheckbox1.setTag(root.getResources().getString(<package>.R.string.checkbox_value_key));
....
setRootTag(root);
// listeners
invalidateAll();
}
错误 根据 George Mount 的说法。等待 Android Studio 2.3 修复该问题
尝试在 CustomView(Group) 中使用 DataBinding
布局如下所示:
<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">
<merge
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/customview_checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="@string/checkbox_value_key"
android:text="My CheckBox"/>
....
</merge>
</layout>
CustomView(扩展 FrameLayout)inflation:
view = DataBindingUtil.inflate((LayoutInflater) getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE), R.layout.custom_view, this, true);
导致此错误:
Error:(46, 61) error: cannot find symbol method getResources()
生成的绑定Class:
public ViewBodyPartsBinding(android.databinding.DataBindingComponent bindingComponent, View[] root) {
super(bindingComponent, root[0], 0);
final Object[] bindings = mapBindings(bindingComponent, root, 9, sIncludes, sViewsWithIds);
this.mboundView0 = (android.widget.ImageView) bindings[0];
this.mboundView0.setTag(null);
this.mboundView2 = (android.support.percent.PercentRelativeLayout) bindings[2];
this.mboundView2.setTag(null);
....
this.customviewCheckbox1 = (android.widget.CheckBox) bindings[1];
this.customviewCheckbox1.setTag(root.getResources().getString(<package>.R.string.checkbox_value_key));
....
setRootTag(root);
// listeners
invalidateAll();
}
错误