2016-01-15 33 views
2

我正在使用Android的綁定庫,我試圖根據布爾值添加或刪除邊距。如果這是真的,我希望TextView在右邊有一個邊距,在左邊沒有邊距,如果不是則相反。所有其他資源工作正常,但是當我編譯代碼時,我得到了關於TextView邊距的這個錯誤:無法找到參數類型爲float的屬性'android:layout_marginRight'的setter。使用Android綁定庫添加維度資源到佈局

任何人都可以發現錯誤?

這是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <variable name="comment" type="mx.com.corpogas.dataModels.FeedbackComment"/> 
     <import type="android.view.View"/> 
    </data> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@{comment.isMine ? @drawable/comment_background_white : @drawable/comment_background_green}" 
      android:textSize="15sp" 
      android:textColor="@{comment.isSent ? (comment.isMine ? @color/colorPrimaryDark : @android:color/white) : @color/unsent_text}" 
      android:layout_marginRight="@{comment.isMine ? @dimen/feedback_comment_margin : @dimen/feedback_comment_no_margin}" 
      android:layout_marginLeft="@{comment.isMine ? @dimen/feedback_comment_no_margin : @dimen/feedback_comment_margin}" 
      android:text="@{comment.content}"/> 


</layout> 

這是我的利潤:

<dimen name="feedback_comment_margin">16dp</dimen> 
<dimen name="feedback_comment_no_margin">0dp</dimen> 

當我刪除的程序編譯和運行完美的利潤率。

回答

9

儘管您可以自己添加它們,但不支持數據綁定佈局屬性。問題在於,這些可能很容易被試圖製作動畫的人濫用。爲了實現這些爲您的應用程序,創建一個綁定的適配器:

@BindingAdapter("android:layout_width") 
public static void setLayoutWidth(View view, int width) { 
    LayoutParams layoutParams = view.getLayoutParams(); 
    layoutParams.width = width; 
    view.setLayoutParams(layoutParams); 
} 
+1

同樣的問題,答案http://stackoverflow.com/questions/34832578/android-databinding-how-to-get-dimensions-from-dimens -xml! –