使用Kotlin XML中的DataBinding Concat本地化字符串和动态字符串

问题描述 投票:-5回答:1

我的XML片段中有一个TextView,它需要使用动态字符串从我的strings.xml文件中连接一个字符串。最初,我尝试过:

android:text='@{"@string/courses.finalGrade" + ": " + course.grade + "%"}'

这预览工作正常,我看到一个格式的字符串:Final Grade: 90%

但是在模拟器中,它打印出@string/courses.finalGrade: 95%

有没有办法从strings.xml文件和xml片段中的动态字符串连接字符串?

android android-layout kotlin android-databinding
1个回答
1
投票

Android数据绑定支持字符串格式。如果您定义文本资源,例如:

<string name="some_text_id">"Some text about final grade: %1$s"</string>

然后,您可以将此文本资源与数据绑定一起使用

android:text="@{@string/some_text_id(course.grade)}"

它与以编程方式使用String.format(...)相同。

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