【问题标题】:Add dimen resource to layout using Android Binding Library使用 Android 绑定库将维度资源添加到布局
【发布时间】:2016-01-15 18:29:32
【问题描述】:

我正在使用 Android 的绑定库,我正在尝试根据布尔值在 TextView 上添加或删除边距。如果是真的,我希望 TextView 在右边有一个边距,在左边没有边距,如果没有,则相反。所有其他资源都可以正常工作,但是当我编译代码时,我收到有关 TextView 上边距的错误:找不到参数类型为 float 的属性“android:layout_marginRight”的设置器。

谁能发现错误?

这是我的 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>

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

【问题讨论】:

    标签: android xml data-binding


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    这是 Kotlin 中的示例。通过 Android 数据绑定库在 ContraintLayout 中动态设置边距。使用 dimens.xml 是因为将值直接放在绑定表达式中会导致语法错误:msg:Syntax error: extraneous input 'p' expecting {}

    布局:

    <android.support.constraint.ConstraintLayout
         [...]
         android:layout_marginBottom="@{viewModel.isMarginVisible ? @dimen/default_margin : @dimen/no_margin}"
    </>
    

    MarginBindingAdapter:

    /**
     *  Data Binding adapters for UI margins
     */
    @JvmStatic
    @BindingAdapter("android:layout_marginTop")
    fun ViewGroup.setMarginTopValue(marginValue: Float) =
        (layoutParams as ViewGroup.MarginLayoutParams).apply { topMargin = marginValue.toInt() }
    @JvmStatic
    @BindingAdapter("android:layout_marginBottom")
    fun ViewGroup.setMarginBottomValue(marginValue: Float) =
        (layoutParams as ViewGroup.MarginLayoutParams).apply { bottomMargin = marginValue.toInt() }
    
    @JvmStatic
    @BindingAdapter("android:layout_marginStart")
    fun ViewGroup.setMarginStartValue(marginValue: Float) =
            (layoutParams as ViewGroup.MarginLayoutParams).apply { leftMargin = marginValue.toInt() }
    
    @JvmStatic
    @BindingAdapter("android:layout_marginEnd")
    fun ViewGroup.setMarginEndValue(marginValue: Float) =
            (layoutParams as ViewGroup.MarginLayoutParams).apply { rightMargin = marginValue.toInt() }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多