简体   繁体   English

绑定来自 XML 文件的格式化字符串,数据绑定无法正常工作

[英]Binding formatted strings from XML file with data binding not working correctly

When developing with data binding to bind formatted strings directly in the XML file, the @{@string/stringName} does not work as normal attribute @string/stringName without data binding, and neither does @{String.format(@string/stringName)} .在使用数据绑定进行开发以直接在 XML 文件中绑定格式化字符串时, @{@string/stringName}在没有数据绑定的情况下不能作为普通属性@string/stringName工作, @{String.format(@string/stringName)}

I would like to know how to use data binding to use formatted strings such as <string name="my_string"> <font face="font_name" color="hex_color"> my text here with %s </font> </string> along with data binding, because @{@string/my_string("My name")} doesn't work correctly.我想知道如何使用数据绑定来使用格式化字符串,例如<string name="my_string"> <font face="font_name" color="hex_color"> my text here with %s </font> </string>连同数据绑定,因为@{@string/my_string("My name")}不能正常工作。 I mean, it does appear with the "My name" inserted on the string, but no formatted attribute like font or color.我的意思是,它确实在字符串中插入了“我的名字”,但没有像字体或颜色这样的格式化属性。

If I don't use data binding and just @string/myName the formatted attributes font and color work propperly, but I don't get the input for %s .如果我不使用数据绑定而只是@string/myName格式化属性字体和颜色正常工作,但我没有得到%s的输入。

Thank you all.谢谢你们。

For anyone still looking for this, you just have to call Span.fromHtml() in the XML file.对于仍在寻找此功能的任何人,您只需在 XML 文件中调用 Span.fromHtml()。 Also don't forget to import it.也不要忘记导入它。

@{Span.fromHtml(@string/stringName("Julio"))}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM