Android绑定双值以使用字符串资源进行查看

时间:2017-08-02 17:09:37

标签: android data-binding android-databinding

尝试使用字符串资源将double值绑定到view。

尝试1:

layout.xml

android:text="@{@string/user_popularity(Double.toString(user.popularity))}"

string.xml

<string name="user_popularity">Popularity: %s</string>

尝试2:

layout.xml

android:text="@{String.format(@string/user_popularity, user.popularity)}"

string.xml

<string name="user_popularity">Popularity: %.1f</string>

错误:

Error:(167, 107) error: double cannot be dereferenced

以下是一些类似的问题

  1. How do I format a double using Android view data bindings?

  2. How do I use databinding to combine a string from resources with a dynamic variable in XML?

1 个答案:

答案 0 :(得分:3)

实际上这很简单,我很糟糕我做错了。 这是解决方案。

<string name="user_popularity">Popularity: %.1f</string>
android:text="@{@string/user_popularity(user.popularity)}"