首页 > 解决方案 > 如何使用 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.

标签: kotlinviewmodelandroid-databinding

解决方案


您在 MutableLiveData 中有维度 id 而不是维度值,但我认为您不能在 xml 中执行此操作,因为视图没有边距设置器。您可以使用 MarginLayoutParams 在代码中执行此操作。


推荐阅读