标签:android android-databinding
我有一个颜色代码存储为一个名为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">
但它无法编译错误:
Error:(80, 35) Cannot find the setter for attribute
‘android:background’ with parameter type java.lang.String on
android.widget.LinearLayout.
解决方法:
因为没有像View.setBackground(“#f0f”)这样的方法!
相反,您可以在SpaceBean.getSelectedThemeColor()中返回Drawable或ARGB颜色值.
例如:
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);
}
}
标签:android,android-databinding 来源: https://codeday.me/bug/20190628/1311498.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。