数据自动转换
<variable
name="time"
type="java.util.Date" />
<variable
name="bg"
type="java.lang.String" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{time}" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@{bg}"
/>
@BindingConversion
fun convertDate(date: Date?): String? {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
return sdf.format(date)
}
@BindingConversion
fun converColor(value: String?): Drawable? {
return ColorDrawable(Color.parseColor(value))
}
bindingBinding.time= Date()
bindingBinding.bg="#ffeeff"
网友评论