假设我有一个Earthquake
有一个字段的类public final double magnitude;
,我有一个与此类似的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="earthquake" type="com.example.Earthquake"/>
</data>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{String.format(earthquake.magnitude)}" />
...
</LinearLayout>
</layout>
注意我必须使用"@{String.format(earthquake.magnitude)}"
使用此字段,否则我会收到此错误:
在参数类型为 double 的 android.widget.TextView 上找不到属性“android:text”的设置器。
不幸的是,这会导致双精度打印。如何格式化显示的双精度值?
我还没有在 M SDK 预览中查看绑定表达式语言,所以我可能会草率下结论,但如果这调用到正常情况String.format()
方法,那么就需要一个模式。你试过这个吗?
android:text='@{String.format("%.1f", earthquake.magnitude)}'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)