kotlin - 如何使用 LiveData 和 BindingAdapter 更改按钮 marginEnd?
问题描述
如何使用 LiveData 和 BindingAdapter 更改按钮 marginEnd?我在 ViewModel 中的 LiveData:
var marginRight = MutableLiveData(R.dimen.default_margin)
我的维度
<dimen name="default_margin">141dp</dimen>
和我的属性:android:layout_marginEnd="@{viewmodel.marginRight}"
在这种情况下,我得到
Cannot find a setter for <android.widget.Button android:layout_marginEnd> that accepts parameter type 'androidx.lifecycle.MutableLiveData<java.lang.Integer>'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
解决方案
您在 MutableLiveData 中有维度 id 而不是维度值,但我认为您不能在 xml 中执行此操作,因为视图没有边距设置器。您可以使用 MarginLayoutParams 在代码中执行此操作。
推荐阅读
- c# - MVVMCross WPF:如何在视图中显示视图?
- javascript - Graphql - 如何获取所选字段的类型(字符串、整数、ID 等)
- json - 通过来自另一个 dict 的正则表达式匹配键更新值
- python - 通过并行处理将一个 csv 拆分为多个压缩 CSV 文件的有效方法
- powershell - PowerShell 和 CSV:阻止 CSV 将文本数据转换为科学记数法
- javascript - SyntaxError: 不能在模块外使用 import 语句-> pupa 包
- swift - 如果初始窗口没有标题栏,如何关闭它
- mysql - 连接mysql中的两个表,其中一个值对应另一个表中的两个值
- java - 在 Selenium 中使用 Gmail API 无法正常获取电子邮件主题
- python - 在 macOS [python, PySimpleGUI, Tkinter, macOS] 中通过 tkinter 非常慢的窗口刷新(更新)