0

我已经声明了一个 bindingadapter,如下所示:

@BindingAdapter(value = ["textToFormat", "numberText"], requireAll = false)
fun setFormattedValue(view: TextView, textToFormat: Int?, numberText: String?) {
    if(textToFormat != null && numberText != null) {
        view.text = String.format(view.context.resources.getString(textToFormat), numberText)
    }
}

我在 xml 中使用它作为

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="14dp"
            android:fontFamily="sans-serif-medium"
            android:gravity="end"
            android:letterSpacing="0.04"
            android:textColor="#555555"
            android:textSize="16sp"
            android:textStyle="normal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Total Contacts : 22"
            app:numberText="211"
            app:textToFormat="@string/total_contacts_summary" />

但是当我尝试构建时,我收到以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     AAPT: /Users/parashar/Documents/work/test/app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/fragment_contacts_summary.xml:21: error: attribute numberText (aka com.example.test:numberText) not found.
     /Users/parashar/Documents/work/test/app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/fragment_contacts_summary.xml:21: error: attribute textToFormat (aka com.example.test:textToFormat) not found.
     error: failed linking file resources.

如果有人能帮我解决这个问题,我会很感激吗?

4

1 回答 1

0

尝试这个

 app:numberText="@{`211`}"
 app:textToFormat="@{@string/total_contacts_summary}"
于 2019-10-23T11:55:56.853 回答