Android使用字符串资源绑定(bind)双值来查看

标签 android data-binding android-databinding

尝试使用字符串资源将 double 值绑定(bind)到 View 。

尝试 1:

布局.xml

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

字符串.xml

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

尝试 2:

布局.xml

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

字符串.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?

最佳答案

其实很简单,可惜我方法不对。 这是解决方案。

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

关于Android使用字符串资源绑定(bind)双值来查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45467052/

相关文章:

android - 最新 Android Studio 更新 3.3.2 后 Unresolved DataBinding 依赖关系

android - 如何单元测试(junit)Android Flavor/Variant - 类不存在

angularjs - 为什么对 $scope 字段的更改不会传播到另一个字段?

javascript - 更新 Handlebars 数据而不重新生成整个模板

c# - .Net 中基于列的数据绑定(bind)?

使用数据绑定(bind)时 Android EditText 不更新

带有 RecyclerView 的 Android BottomSheet

Android三重缓冲-预期行为?

java - 同一父类的许多对象的 LibGdx 对象池

Android 数据绑定(bind) - 错误 :(119, 29) 标识符必须具有来自 XML 文件的用户定义类型。 main_radio_subscribe 丢失了