2016-01-15 40 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! –