Android String.xml% problem

Source: Internet
Author: User

After repeated checks, it is found that the% in String.xml causes the compilation to fail,

This is due to the new SDK with the new version of AAPT (Android Project compiler), this version of AAPT compiled will be more stringent than the old version, and then in the Android latest development document description of the string section, has explained how to set the%s and other symbols, Here is the document fragment:

If you need to format your strings using String.Format (String, Object ...), then the can do so by putting your format Argu ments in the string resource. For example, with the following resource:

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:

<string name= "Welcome_messages" >hello,%1$s! You have%2$d new messages.</string>

In this example, the format string has arguments:%1 $ s is a string and%2$d is a decimal number. You can format the string with arguements from your application like this:

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:

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.

Supplementary questions:

Some readers asked how to use the% number in <string></string>

There are two ways to choose
1. Use a percentage of percent to represent 1%, and the use of the signifier symbol \ is the same 2. If your string does not need to be formatted, you can add a property to your <string tag: formatted= "false" such as <string name= "test" Formatted= "false" >% test%</string>

Android String.xml% problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.