使用Android绑定库将Dimen资源添加到布局

Car*_*s J 3 xml data-binding android

我正在使用Android的绑定库,我试图在TextView上添加或删除边距,具体取决于布尔值.如果这是真的,我希望TextView在右边有一个边距,在左边没有边距,如果不是则相反.所有其他资源工作正常,但是当我编译代码时,我得到关于TextView上的边距的错误:无法找到参数类型为float的属性'android:layout_marginRight'的setter.

谁能发现错误?

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的利润:

<dimen name="feedback_comment_margin">16dp</dimen>
<dimen name="feedback_comment_no_margin">0dp</dimen>
Run Code Online (Sandbox Code Playgroud)

当我删除边距时,程序编译并运行完美.

Muk*_*ana 10

不支持布局属性的数据绑定,但您可以自己在技术上添加它们.问题是这些可能很容易被试图动画它们的人滥用.要为您的应用程序实现这些,请创建绑定适配器:

@BindingAdapter("android:layout_width")
public static void setLayoutWidth(View view, int width) {
  LayoutParams layoutParams = view.getLayoutParams();
  layoutParams.width = width;
  view.setLayoutParams(layoutParams);
}
Run Code Online (Sandbox Code Playgroud)

  • 与http://stackoverflow.com/questions/34832578/android-databinding-how-to-get-dimensions-from-dimens-xml一样的问答! (2认同)