如何解决如何从viewModel中获取xml中的属性视图
我有3个文件(活动,ViewModel,XML)
我使用MVVM并将XML连接到ViewModel。
val_mape
ViewModel.class
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.MainViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/priceTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={viewModel.price}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
在活动中,我将XML连接到以查看模型
open class MainViewModel : ViewModel() {
val price = MutableLiveData<String>()
val priceTextViewWidth = MutableLiveData<Int>()
}
我可以在View Model中使用更改价格变量来更新Textview,但是 我需要将TextView宽度传递给ViewModel中的priceTextViewWidth 。
解决方法
替换:
android:layout_width="wrap_content"
使用
android:layout_width="@{viewModel.priceTextViewWidth}"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。