簡體   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