Android XML Percent Symbol

2019-01-02 22:04发布

I have an array of strings in which the % symbol is used. Proper format for using the % is %. When I have a string in that array with multiple % it gives me this error.

 Multiple annotations found at this
 line:
 - error: Multiple substitutions specified in non-positional format;
   did you mean to add the formatted="false" attribute?
 - error: Found tag </item> where </string-array> is expected

14条回答
仙女界的扛把子
2楼-- · 2019-01-02 22:22

Try using a backslash in front of it, like below:

\%
查看更多
我只想做你的唯一
3楼-- · 2019-01-02 22:23

Use

<string name="win_percentage">%d%% wins</string>

to get

80% wins as a formatted string.

I'm using String.format() method to get the number inserted instead of %d.

查看更多
淡お忘
4楼-- · 2019-01-02 22:24

Suppose you want to show (50% OFF) and enter 50 at runtime. Here is the code:

<string name="format_discount"> (
<xliff:g id="discount">%1$s</xliff:g>
<xliff:g id="percentage_sign">%2$s</xliff:g>
 OFF)</string>

In the java class use this code:

String formattedString=String.format(context.getString(R.string.format_discount),discountString,"%");
holder1.mTextViewDiscount.setText(formattedString);
查看更多
Fickle 薄情
5楼-- · 2019-01-02 22:26

You can escape the % in xml with %%, but you need to set the text in code, not in layout xml.

查看更多
闹够了就滚
6楼-- · 2019-01-02 22:29

To allow the app using formatted strings from resources you should correct your xml. So, for example

<string name="app_name">Your App name, ver.%d</string>

should be replaced with

<string name="app_name">Your App name, ver.%1$d</string>

You can see this for details.

查看更多
来,给爷笑一个
7楼-- · 2019-01-02 22:29

Try this one (right):

<string name="content" formatted="false">Great application %s  ☞  %s  ☞  %s \\n\\nGo to download this application %s</string>
查看更多
登录 后发表回答