Android绑定适配器在自定义视图上无法工作

8
 <com.myapp.view.widgets.TextImageTextView
            android:id="@+id/tv_on_boarding_step_one"

            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="1"
            app:layout_constraintBottom_toTopOf="@+id/appCompatButton"
            app:layout_constraintEnd_toStartOf="@+id/space_one_two"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label_description"
            app:textAbove="Nitin" />

这是我在xml中的自定义视图,下面是将textAbove绑定到此视图的方法。
@BindingAdapter("app:textAbove")
fun setTextAbove(view: TextImageTextView, textAbove: Int) {
    // this is the method I have in my custom view
    view.setTextAbove(view.context.getString(textAbove))
}

但是当我编译时,出现了错误:错误:找不到属性'app:textAbove'。

2个回答

18

改变

app:textAbove="Nitin"

app:textAbove='@{"Nitin"}'

另一种方式

// app:textAbove="@{@string/nitin}"  text from String resource
// app:textAbove="@{viewModel.nitin}" text from ViewModel

1
谢谢。它帮助了我 :) - Nitin Mesta
2
感谢您节省了我的时间。 - Sup.Ia
@Linh: 你能否看一下类似的问题:https://dev59.com/LG4NtIcB2Jgan1znd_yI - SVK

-1

在xml中添加这行代码:

 xmlns:app="http://schemas.android.com/apk/res-auto"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接