2016-01-28 32 views
5

私はカスタムのbindingAdapterをアンドロイドで作成しました。色を渡すときに色を変更したいと思っています。 。ここでは、コードは次のようになります。ここ はデータバインディングの私の見解モデルである:Android DataBinding - @BindingAdapterカスタムのアプリケーション名前空間が無視されています

SETTEXTCOLORように色を渡すことが予想このモデルイムにバインドされ、私のxmlファイルで今すぐ
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")); 

} 

}

この方法は、それを使用することができます:

<?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> 

私は、ランタイム時にエラーを取得しています:

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. 

bindingAdapterのものを取り出すと、他のデータバインディングのものと完全に機能します。そのカスタムバインディングは機能しません。私のプロジェクトのタイトルはfloatingactionbuttonbasic btwです。

+0

警告を解決しましたか? '警告:属性app:bindColorのアプリケーション名前空間は無視されます。 ' – Calin

+0

ああ、気にしない、この男はそれを解決しましたhttps://stackoverflow.com/questions/35313466/android-databinding-custom-binding-adapter-warning – Calin

答えて

11

私はこれを動作させることができました。問題は、私が文字列をどうやって渡しているかということでした。それはその周りに ``あるべきです。

app:bindColor="@{`green`}" 

あなたもこれを行うことができます。

app:bindColor='@{"green"}' 

をしかし、何が許されていないように思われることは、この表記法です:

app:bindColor="@{'green'}" 

私が興味を持った場合に他の人を助けるためにblog about itを書きました。

+0

マークしてくださいあなたの答えは答えたので、質問は未回答のタブにもう表示されません – Tseng

+0

私はあなた..... –

関連する問題