情况:
在string.xml里写项目
<string name="current_question_in_total_str">%s/%s 题</string>时出错,提示
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the
formatted="false" attribute?
- error: Unexpected end tag string这样写本意是想利用String.format(String, Object)来格式化字符串。
解决:
百度和查文档得知,如果作为格式化字符串的话,写法应该为
Formatting strings
If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
例子中
Hello, %1$s! You have %2$d new messages.
有两个参数,第1个为%s,第2个为%d
猜想数字1、2对应参数号
如果不想让文本作为格式化字符串,则需添加参数
formatted="false"
如
<string name="test" formatted="false">%test%</string>

本文介绍在Android项目的string.xml文件中如何正确使用字符串格式化功能,包括格式化字符串的基本用法和避免常见错误的方法。
582

被折叠的 条评论
为什么被折叠?



