[英]How to write binding adapter to set background colour when the colour is coming from api as hex code?
背景颜色来自API以这种形式FFF9E6.我正在使用数据绑定。我无法理解如何将其设置为背景颜色。 我相信绑定适配器可以工作,但不知道如何写它。 我怎么能写相同的绑定适配器?
几个月前我遇到了同样的问题。 我所做的是将来自API的数据存储到一个POJO类中。 然后在一个XML文件中使用该POJO类成员作为数据绑定变量。 即
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_margin="@dimen/_8dp"
android:layout_weight="1"
android:gravity="center"
android:padding="@dimen/_8dp"
app:color="@{item.color}" />
它是绑定适配器
@BindingAdapter("bind:color")
public static void setStatus(TextView textView, String color) {
}
这段代码适用于我的情况。
@BindingAdapter("android:backgroundColor")
fun ViewGroup.setBackground(backgroundColor: String) {
val color: Int = try {
Color.parseColor(background)
} catch (e: Exception) {
Color.parseColor("#$background")
}
setBackgroundColor(color)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.