Android 绑定(bind)适配器不适用于 CustomView

标签 android data-binding kotlin android-custom-view

 <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 中的自定义 View

以及将 textAbove 绑定(bind)到此 View 的方法

@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”。

最佳答案

改变

app:textAbove="Nitin"

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

另一种方式

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

关于Android 绑定(bind)适配器不适用于 CustomView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47449825/

相关文章:

java - SWT 列表的 E4 数据绑定(bind)

android - Android按钮-聆听者的差异

android - 从另一个 fragment 导航回来时自定义 View 的奇怪行为

android - Jetpack Navigation 中从 RecyclerView 到 Detail Fragment 的共享元素转换

android - 如何从 SQLite 获取最近 3 小时的数据

java - import android.support 无法解决

android - 如何通过使用 Intent 导航到 'android.intent.action.MAIN' Activity ?

apache-flex - 如何在 Flex 3 中创建双向数据绑定(bind)?

data-binding - “UpdateSourceTrigger=PropertyChanged” 等效于 WinRT-XAML 中的文本框

java - 安卓 : faster solution to copying one file to another