android 使用字符串颜色代码对 android:background 属性进行数据绑定
android databinding on android:background attribute using string color code
我有一个颜色代码作为字符串存储在一个名为 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")!
这样的方法
相反,您可以 return 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);
}
}
我有一个颜色代码作为字符串存储在一个名为 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")!
这样的方法相反,您可以 return 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);
}
}