Android 数据绑定:有没有办法限制observeInt、@ColorInt或@ColorRes的整数类型

Android 数据绑定:有没有办法限制observeInt、@ColorInt或@ColorRes的整数类型,android,binding,Android,Binding,当我们使用Android数据绑定库时,我们总是这样写: // layout xml android:textColor="@{vm.textColor}" // SomeVm.kt var textColor = ObservableInt() ... textColor.set(R.color.some_color) 但这是错误的。TextView.setTextColor()需要@ColorInt参数,如: public void setTextColor(@ColorInt int c

当我们使用Android数据绑定库时,我们总是这样写:

// layout xml
android:textColor="@{vm.textColor}"

// SomeVm.kt
var textColor = ObservableInt()
...
textColor.set(R.color.some_color)
但这是错误的。TextView.setTextColor()需要@ColorInt参数,如:

public void setTextColor(@ColorInt int color);
但是我们设置的是@ColorRes int资源

所以我的问题是,有没有办法将
textColor
变量限制为仅@ColorInt可观察int

我试过:

var textColor = ObservableField<@ColorInt Int>()

我认为这不是限制,而是为
setTextColor
方法提供正确的参数类型

您是否尝试过使用
textColor.set(ContextCompat.getColor(context,R.color.some_color))
来获得
@ColorInt
?这应该行得通

否则,创建您自己的
BindingAdapter
,例如:

@BindingAdapter("textColor")
fun setTextColor(view: TextView, @ColorRes color: Int) = with(view) {
    textColor = ContextCompat.getColor(context, color)
}

并在布局中使用它,如
app:textColor=“@{vm.textColor}”
我认为这不是限制,而是为
setTextColor
方法提供正确的参数类型

您是否尝试过使用
textColor.set(ContextCompat.getColor(context,R.color.some_color))
来获得
@ColorInt
?这应该行得通

否则,创建您自己的
BindingAdapter
,例如:

@BindingAdapter("textColor")
fun setTextColor(view: TextView, @ColorRes color: Int) = with(view) {
    textColor = ContextCompat.getColor(context, color)
}
并在布局中使用它,如
app:textColor=“@{vm.textColor}”