繁体   English   中英

数据绑定时的自定义属性设置器错误 - Android

[英]custom attribute setter error when data binding - Android

我有自定义属性,它被声明为字符串类型,当在 xml 中将值作为字符串传递时,就像这个app:cpb_title="sometsring"它工作但是当我尝试像这个app:cpb_title"@{model.someStringField}"它给了我错误“找不到接受参数类型 java.lang.String 的“app:cpb_title”的设置器”

我该如何解决?

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>

内部视图 class

 String t = a.getString(R.styleable.CircularProgressBar_cpb_title);
    if(t!=null)
        mTitle = t;

您可以使用BindingAdapter而不是在attrs.xml文件中声明属性。 请执行以下操作:

class BindingUtil {

    @BindingAdapter("cpb_title")
    public static void setTitle(TextView view, String title) {
        view.setText(title);
    }
}

然后,您可以在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM