Android dataBinding - 忽略@BindingAdapter自定义应用程序命名空间

j2e*_*nue 6 data-binding android

我已经在android中创建了一个自定义的bindingAdapter,当我传递颜色时,我想要改变颜色,这实际上是为了测试我正在努力确保它的工作原理.这是代码:这是我的视图数据绑定的模型:

public class User {
public ObservableInt visible;

public User(int visible) {
    this.visible=new ObservableInt(visible);
}


@BindingAdapter({"app:bindColor"}) //notice the bindColor custom attribute
public static void setTextColor(TextView view,String color) {

    if("green".equals(color))
    view.setTextColor(Color.parseColor("#63f421"));

}
Run Code Online (Sandbox Code Playgroud)

}

现在在我的xml文件中绑定到此模型,我希望传递一种颜色,以便setTextColor方法可以使用它:

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <data class="MainActivityBinder">
        <variable name="user" type="com.example.android.floatingactionbuttonbasic.User"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_one"
            android:text="my first textview"/>

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_two"
            android:text="my second textview"
            android:visibility="@{user.visible}"
            app:bindColor="@{'green'}" //see im passing in the green string here
            android:textColor="@android:color/holo_green_dark"/>

    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

我在运行时收到错误:

Error:(27, 65) error: package com.example.android.floatingactionbuttonbasic.databinding does not exist
Warning:Application namespace for attribute app:bindColor will be ignored.
Error:(24, 33) Identifiers must have user defined types from the XML file. een is missing it 
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
Run Code Online (Sandbox Code Playgroud)

如果我取出bindingAdapter的东西,它与其他数据绑定的东西完美配合.它只是这个自定义绑定不起作用.我的项目名为floatingactionbuttonbasic btw.

j2e*_*nue 12

我能够让这个工作.问题是我如何传递字符串.它应该有"围绕它".

app:bindColor="@{`green`}"
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

app:bindColor='@{"green"}'
Run Code Online (Sandbox Code Playgroud)

但似乎不允许的是这种表示法:

app:bindColor="@{'green'}"
Run Code Online (Sandbox Code Playgroud)

如果有兴趣,我写了一篇关于它博客来帮助别人.