我将一个颜色代码存储为名为bean
的数据对象中的字符串,如下所示:
public class SpaceBean extends BaseObservable {
private String selectedThemeColor;
@Nullable
@Bindable
public String getSelectedThemeColor() {
return selectedThemeColor;
}
public void setSelectedThemeColor(String selectedThemeColor) {
this.selectedThemeColor = selectedThemeColor;
notifyPropertyChanged(BR.selectedThemeColor);
}
}
我想在线性布局android:background
属性中使用数据绑定表达式,如:
<LinearLayout
android:id="@+id/ll_space_detail_overlay"
android:layout_width="match_parent"
android:layout_height="144dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/common_margin"
android:layout_marginEnd="@dimen/common_margin"
android:layout_marginStart="@dimen/common_margin"
android:background="@{bean.selectedThemeColor}"
android:clickable="true"
android:orientation="vertical"
android:padding="@dimen/common_padding"
android:visibility="visible">
但它无法编译错误:
错误:(80,35)无法找到属性的setter &#39;机器人:背景&#39;参数类型为java.lang.String android.widget.LinearLayout。
答案 0 :(得分:3)
例如:
public class SpaceBean extends BaseObservable {
private String selectedThemeColor;
@Nullable
@Bindable
public Drawable getSelectedThemeColor() {
return new ColorDrawable(Color.parseColor(selectedThemeColor));
}
public void setSelectedThemeColor(String selectedThemeColor) {
this.selectedThemeColor = selectedThemeColor;
notifyPropertyChanged(BR.selectedThemeColor);
}
}