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

鲢鱼

我有一个颜色代码以字符串形式存储在一个数据对象中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)在android.widget.LinearLayout上找不到参数类型为java.lang.String的属性“ android:background”的设置器。

点子

因为没有像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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android提示的颜色代码是什么?

计算任意字符串的十六进制颜色代码

Android数据绑定上的字符串比较

MvvmCross Android绑定字符串数组

Swift:将字符串转换为十六进制颜色代码

Android绑定双精度值以使用字符串资源进行查看

为什么使用Xamarin在Android上进行XML序列化会创建以(char)65279开头的字符串

我们可以在Android中使用字符串资源进行代码注释吗?

使用字符串访问属性

Android如何在带有数据绑定的TextView中使用字符串变量

在Android中的Java文件中使用字符串

使用字符串问题android

如何使用字符串作为代码?

Android使用字符串数组填充微调器

Android / Java:创建颜色的字符串?

Android:颜色代码

使用字符串与属性名称进行角度绑定

从任意字符串生成颜色代码

如何通过使用 PHP 交换字符串中的 2 个字符来动态生成颜色代码

无法使用字符串插值访问 Firestore 数据的 JSON 属性

如何替换颜色代码字符串以外的哈希标签

使用字符串在角度 4 中进行 2 向绑定

如何在可跨字符串中设置字体颜色代码?

将字符串中的十六进制颜色代码替换为彩色 html 标签

用字符串对数据框的值进行分组

从数据属性获取颜色代码并设置滑块背景

使用 pandas 在列名中使用字符串对大型数据集进行分块

在反应中使用字符串设置数据属性

如何从短字符串中获取颜色代码的十六进制字符