[英]custom attribute setter error when data binding - Android
I have custom attribute which is declared as type of string, when a pass the value as just string in xml like this app:cpb_title="sometsring"
its working but when i try data binding like this app:cpb_title"@{model.someStringField}"
it gaves me error "cannot find setter for "app:cpb_title" that accepts parameter type java.lang.String"我有自定义属性,它被声明为字符串类型,当在 xml 中将值作为字符串传递时,就像这个app:cpb_title="sometsring"
它工作但是当我尝试像这个app:cpb_title"@{model.someStringField}"
它给了我错误“找不到接受参数类型 java.lang.String 的“app:cpb_title”的设置器”
how can i fix it?我该如何解决?
attrs.xml attrs.xml
<declare-styleable name="CircularProgressBar">
<attr name="cpb_hasShadow" format="boolean"/>
<attr name="cpb_progressColor" format="string"/>
<attr name="cpb_backgroundColor" format="string"/>
<attr name="cpb_title" format="string"/>
<attr name="cpb_titleColor" format="string"/>
<attr name="cpb_subtitle" format="string"/>
<attr name="cpb_subtitleColor" format="string"/>
<attr name="cpb_strokeWidth" format="integer"/>
</declare-styleable>
inside view class内部视图 class
String t = a.getString(R.styleable.CircularProgressBar_cpb_title);
if(t!=null)
mTitle = t;
You can use BindingAdapter instead of declaring attributes in the attrs.xml
file.您可以使用BindingAdapter而不是在attrs.xml
文件中声明属性。 Please do as below:请执行以下操作:
class BindingUtil {
@BindingAdapter("cpb_title")
public static void setTitle(TextView view, String title) {
view.setText(title);
}
}
Then, you can use app:cpb_title
attribute in your XML as below:然后,您可以在 XML 中使用app:cpb_title
属性,如下所示:
<Textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cpb_title="@{model.someStringField}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.