questions about percent signs in Android resource filesCategory: Android JAVA2014-08-01 16:53 1345 people read reviews (0) favorite reports Android Format resource file
Directory (?) [+]
Problem
When editing Strings.xml,
Good
[HTML]View Plaincopy
- <string name="Myurl">http://code.dd.com/rr? q=%rr.55</string>
Or
[HTML]View Plaincopy
- <string name= "Message_content '> Congratulations you earn%1s points,%%2s%</string>
Prompt for the following error
[CSS]View Plaincopy
- Multiple annotations found at the line:
- -Error:multiple substitutions specified in non-positional format; do you mean to add
- The formatted="false" attribute?
- -Error:unexpected End Tag String
The reason for this error is mainly because the strings string contains a percent percent (%),
One: There are several ways to solve 1. Use two percent sign to denote a percent semicolon
[HTML]View Plaincopy
- <string name="Myurl">http://code.dd.com/rr? q=%%rr.55</string>
- <string name= "Message_content '> Congratulations you earn%1s points, account for%2s percent</string>
2. Use the escape character to indicate
[HTML]View Plaincopy
- <string name="Myurl">http://code.dd.com/rr? q=\%rr.55</string>
Two: no formatting required
Depending on the error message, if the string does not need to be formatted, you can add the attribute on the <string label: formatted= "false", i.e.
[HTML]View Plaincopy
- <string name="Myurl" formatted="false">HTTP://CODE.DD.COM/RR? q=%rr.55</string>
Three: The extension of the above problem
If you need to use String.Format (String, Object ...) to format your string, you can put the formatting parameters in your string, see the following example:
[HTML]View Plaincopy
- <string name="Welcome_messages">hello,%1$s! You have%2$d new messages. </string>
In this example, the formatted string has 2 parameters,%1 $ s is a string%2$d is a floating-point number, you can use the following method in your program to format the string according to the parameters:
[Java]View Plaincopy
- Resources res = getresources ();
- String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);
So, according to the example, I need to change the%s to%1 $ s, and after the modification, the program starts successfully.
"Go" questions about percent signs in Android resource files