数据绑定布局约束

2024-01-31

我在添加要查看的结束约束时遇到问题

 <android.support.constraint.ConstraintLayout
    android:id="@+id/chatDocumentMessageContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{chatMessage.corespondent==Corespondent.Sent? @drawable/very_rounded_corners_gray_background:@drawable/very_rounded_corners_orange_background}"
    android:maxWidth="300dp"
    android:minWidth="140dp"
    android:onClick="@{clickListener::onClick}"
    android:padding="@dimen/padding_large"
    app:layout_constraintEnd_toEndOf="@{chatMessage.corespondent==Corespondent.Sent? ConstraintSet.PARENT_ID:ConstraintSet.UNSET}">

我收到这个错误:

[kapt] 发生异常: android.databinding.tool.util.LoggedErrorException:找到数据绑定 错误。 ****/ 数据绑定错误 ****msg: 找不到参数类型为 int 的属性“app:layout_constraintEnd_toEndOf”的设置器 android.support.constraint.ConstraintLayout。

尝试添加以下适配器,但仍然不起作用:

 @BindingAdapter(" app:layout_constraintEnd_toEndOf")
    fun setEndConstraint(guideline: Guideline, resource: Int) {
        val params = guideline.getLayoutParams() as ConstraintLayout.LayoutParams
        params.endToEnd = resource
        guideline.layoutParams = params
    }

我为这种情况创建了一个 BindingAdapter:

@BindingAdapter(
    "layout_conditionalConstraint_startSide",
    "layout_conditionalConstraint_toEndId",
    "layout_conditionalConstraint_endSide",
    "layout_conditionalConstraint_condition"
)
fun setConditionalConstraint(
    view: View, startSide: Int, endId: Int, endSide: Int, condition: Boolean
) {
    val constraintLayout = (view.parent as? ConstraintLayout) ?: return
    with(ConstraintSet()) {
        clone(constraintLayout)
        if (condition) connect(view.id, startSide, endId, endSide)
        else clear(view.id, startSide)
        applyTo(constraintLayout)
    }
}

在你的情况下,你可以这样使用它:

<android.support.constraint.ConstraintLayout
    android:id="@+id/chatDocumentMessageContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{chatMessage.corespondent == Corespondent.Sent ? @drawable/very_rounded_corners_gray_background : @drawable/very_rounded_corners_orange_background}"
    android:maxWidth="300dp"
    android:minWidth="140dp"
    android:onClick="@{clickListener::onClick}"
    android:padding="@dimen/padding_large"
    app:layout_conditionalConstraint_startSide="@{ConstraintSet.END}"
    app:layout_conditionalConstraint_toEndId="@{ConstraintSet.PARENT_ID}"
    app:layout_conditionalConstraint_endSide="@{ConstraintSet.END}"
    app:layout_conditionalConstraint_condition="@{chatMessage.corespondent == Corespondent.Sent}">

但在大多数情况下,您应该能够使用 ConstraintLayout 提供的功能获得所需的布局,此 BindingAdapter 仅在少数极端情况或复杂布局中有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据绑定布局约束 的相关文章

随机推荐

  • iOS 与 IPv6 和 Azure 的问题

    我们的 Xamarin iOS 应用程序之一被拒绝 因为 Service URI 似乎无法从 IPv6 网络获得 从 2016 年 6 月 1 日开始 Apple 希望所有 iOS 应用程序在纯 IPv6 网络中完全兼容 Microsoft
  • 快速检索 WKInterfaceLabel 的文本

    如何在 WatchKit 中获取 Swift 或 Objective C 中的标签文本 该类不是 UILabel 而是 WKInterfaceLabel 我也尝试过搜索苹果的类库 https developer apple com libr
  • 如何使用 NHibernate 锁定模式在更新之前锁定对象?

    首先让我说明一下我想要实现的目标 我有一张桌子 里面装满了工作 有一个 Web 服务 其方法允许更改作业数据 称为SaveJob 此方法检索作业及其所有数据 对新数据运行验证 这需要对其他表进行一些数据库查询 然后将其保存回数据库 有点慢
  • 静态解析类型参数的深入描述

    我正在寻找对静态解析类型参数的非常全面的审查 它们到底可以做什么 它们的局限性是什么 使用它们的效果是什么 它们如何与普通类型参数结合 以及实例级内联成员如何工作 规范本身对这个主题的介绍很少 只是顺便提到了它们 它没有提到具有此类类型参数
  • 如何屏蔽 Jenkins Pipeline 项目中的密码字段?

    当密码属性定义在Jenkinsfile properties parameters password name KEY description Encryption key 每次执行管道时 Jenkins 都会提示用户提供其值 我希望这个参
  • ADO.NET 数据服务的数据压缩

    我有一个由 NET 应用程序 不是 IIS 公开的 ADO NET 数据服务 该服务由 NET 客户端应用程序使用 对此数据服务的某些调用会返回大量数据 我想压缩传输中的 XML 数据以减少负载并提高性能 这可能吗 我假设你是使用 WCF
  • 错误:膨胀类 com.google.android.material.textfield.TextInputLayout

    我收到错误 这是我的 XML 文件
  • 什么情况下endOffset>lastMsg.offset+1?

    Kafka 对于分区返回 endOffset 15 但可以使用的最后一条消息的偏移量为 13 而不是我期望的 14 我想知道为什么 The 卡夫卡文档 https kafka apache org 28 javadoc org apache
  • 用 C/C++ 编译 DLL,然后从另一个程序调用它

    我想制作一个简单的DLL 它导出一个或两个函数 然后尝试从另一个程序调用它 到目前为止我所看到的都是复杂的问题 将事物链接在一起的不同方式 奇怪的问题我什至还没有开始意识到存在 我只是想开始 通过做这样的事情 制作一个导出一些函数的 DLL
  • 测试一个封闭的ios应用程序

    我目前正在尝试处理不同的应用程序状态 关闭 后台或在应用程序的不同选项卡中 但是当我尝试测试应用程序在关闭并收到推送通知时如何工作 双击主页并强制关闭应用程序然后重新打开它 我不确定代码中哪里出了问题 由于我是从手机本身而不是 xcode
  • 使用 Axios 下载文件然后上传到 Amazon S3

    我已经看到关于使用 Axios 下载文件并将其上传到 S3 的各种问题 但没有一个将它们联系在一起 而且我对流 blob 多部分表单等感到困惑 到目前为止 这是我的代码 下载文件 const downloadResponse await a
  • MySQL 中的事务暂停是如何工作的?

    在 Spring 框架手册中 他们声明对于 PROPAGATION REQUIRES NEW 当前事务将被挂起 暂停交易 是什么意思 超时计时器停止对当前事务计数 这种暂停的实际影响是什么 谢谢你 Asaf 这并不意味着什么特别的 暂停的交
  • Mathematica 协助:使用 /。将方法替换为 IF[] 条件

    首先我要声明 我是一名 Mathematica 新手 这个问题可能很容易回答 但到目前为止 我在互联网上找不到任何针对这个特定问题的帮助 在这里 我基本上总结了我需要我的代码做什么 使用时出现一些问题 将命令替换为IF条件语句 基本上 我有
  • Sql Server 2008 - 换行符和全文查询

    我有一个数据库和一个全文索引表 我们称这张表为test 该表有一个名为testfield 现在 我们只插入一条记录 如下所示 insert into test values word Char 13 test 该查询插入一个带有LINE B
  • 如何选择 numpy 二维数组中唯一元素的所有位置及其周围的边界框?

    我有一个 2D numpy 数组 我想找到所有唯一元素的 每个 位置 我们可以使用以下方法找到独特的元素numpy unique numpyarray 这是棘手的部分 现在我必须知道每个独特元素的所有位置 让我们考虑以下示例 array 1
  • MySQL自动增量加上一列中的字母数字

    我是来自 Oracle 的 MySQL 新手 我需要创建一个表 其中主键位于一列中 但格式如下 X A注明原产国的信件 例如S对于西班牙来说 Z津巴布韦等 我们只有五个原籍国 YYYYMMDD 该格式的日期 9999 4 位办公代码 999
  • 奇怪的超时::控制器操作中的 render_to_string 和 HTTParty 出现错误

    在我的控制器操作中 我使用带有 jbuilder 模板的 render to string 来将 http post 操作的对象渲染到 API 此 API 调用的结果是控制器应该渲染的结果 但是在通过 render to string 渲染
  • Apache Commons Math 的标准差

    我正在使用 Apache Commons Math 计算向量的 SD 问题 我得到的值与手工得到的值不同 DescriptiveStatistics stats new DescriptiveStatistics stats addValu
  • iOS 10 iMessage 贴纸应用中 MSSticker Peels 的回调

    我正在 iOS 10 中尝试贴纸 iMessage 应用程序 但遇到了问题override func didStartSending message MSMessage conversation MSConversation 中的方法MSM
  • 数据绑定布局约束

    我在添加要查看的结束约束时遇到问题