代码之家  ›  专栏  ›  技术社区  ›  Nitin Mesta

Android绑定适配器不适用于CustomView

  •  6
  • Nitin Mesta  · 技术社区  · 7 年前
     <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中的自定义视图

    以及将上面的文本绑定到此视图的方法

    @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 回复  |  直到 7 年前
        1
  •  15
  •   Linh    7 年前

    改变

    app:textAbove="Nitin"
    

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

    另一种方式

    // app:textAbove="@{@string/nitin}"  text from String resource
    // app:textAbove="@{viewModel.nitin}" text from ViewModel
    
        2
  •  -1
  •   SahdevRajput74    7 年前

    在xml中添加此行:

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