用户数据绑定到绑定HTML。 fromHtml字符串android

问题描述 投票:0回答:1

嗨,我是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 android-viewmodel android-binder
1个回答
0
投票

这称为two-way data binding

android:text="@={Html.fromHtml(loginviewmodel.accountString)}"

InverseMethod注释可以应用于双向数据绑定中使用的任何方法。因此,如果不需要双向绑定,请在XMl中进行总结,然后使用它。

 android:text="@{Html.fromHtml(loginviewmodel.accountString)}"

这将起作用。 :)

© www.soinside.com 2019 - 2024. All rights reserved.