android上的android数据绑定:使用字符串颜色代码的background属性

android上的android数据绑定:使用字符串颜色代码的background属性,第1张

概述我有一个颜色代码存储为一个名为bean的数据对象中的字符串,如下所示: public class SpaceBean extends BaseObservable { private String selectedThemeColor; @Nullable @Bindable public String getSelectedThemeColor() { 我有一个颜色代码存储为一个名为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数据绑定:使用字符串颜色代码的background属性全部内容,希望文章能够帮你解决android上的android数据绑定:使用字符串颜色代码的background属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1128070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存