【发布时间】:2020-04-15 07:50:12
【问题描述】:
嗨,我是 mvvm 结构的新手,我在这里学习新东西。目前我已将默认字符串与我的视图绑定,我有多种颜色的文本,因此我选择使用 HTML.fomHTml 螺母,我无法使用任何人都可以帮助解决我的测试代码下方的此绑定问题 ....
数据>
<import type="android.text.Html"/>
<variable
name="loginviewmodel"
type="app.road2xtech.neighbourhood.view.viewmodel.LoginViewModel" />
</data>
<TextView
android:id="@+id/textViewAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="56dp"
android:fontFamily="@font/raleway_regular"
android:gravity="center"
android:text="@={Html.fromHtml(loginviewmodel.accountString)}"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
它会给我以下错误
The expression 'android.text.Html.fromHtml(loginviewmodelAccountString)' cannot be inverted, so it cannot be used in a two-way binding
Details: There is no inverse for method fromHtml, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
【问题讨论】:
-
这样试试 android:text="@{Html.fromHtml(loginviewmodel.accountString)}"
-
也停止在 xml 中编写代码。这是一种不好的做法,而且很难调试。
-
@Black4Guy 是的,这对我有用
标签: android android-viewmodel android-binder