关于string.xml的一些技巧

Android2.3以后, 更新ADT和Android SDK后,发现编译出现问题,错误提示:

    - error: Unexpected end tag string

string.xml中指向 %s 存在问题。

网上找了半天,说是由于新的SDK采用了更加严格的新版本项目编译器, 摘录点说明:

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 arguements from your application like this:

 

 %1$s是字符串 %2$d 是浮点数,%后面的数字代表先后顺序。可以用以下代码来格式化字符串:

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

具体这么做的理由:

http://bobkerns.typepad.com/bob_kerns_thinking/2010/12/dear-java-better-text-formatting-please.html

 

 

另外xml中有一些转义字符,android各版本中可用。

示例:

<string name="menu_black_white">Black &amp; White</string>

 

附个xml中常用的转义字符:

<              &lt;

>              &gt;

&              &amp;

' (单引号)   &apos;

"(双引号)   &quot;

 

 

接下来还有一个东西,项目中碰到过,顺带一提:

之前有个项目中需要用到角度符号° 然后我非常自然地把这个符号复制粘帖到xml里了, 试了下还不错. 后来发现其他一些设备却会显示错误. 汗... 然后...然后就找到这些:

在xml中可以用unicode来表示一些特殊字符, 比如"..."可以用/u2026来表示, 而"°"可以用/u00B0来表示.

具体查表, 给个网址:

http://www.nengcha.com/code/unicode/class/2/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值