加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

string.xml 中显示特殊符号

发布时间:2020-12-15 23:34:15 所属栏目:百科 来源:网络整理
导读:项目中要在string.xml 中显示特殊符号,如@号冒号等,直接写肯定不行啦。。只能考虑使用ASCII码进行显示: @号 #064; :号 #058; 空格 #160; 以下为常见的ASCII十进制交换编码: #032;-- -- 这边是空格 #033;--!-- #034;--"-- xml中需要在前面加入 #035;--#-
项目中要在string.xml 中显示特殊符号,如@号冒号等,直接写肯定不行啦。。只能考虑使用ASCII码进行显示:

@号 @
:号 :
空格  
以下为常见的ASCII十进制交换编码:
&#032;--> <-- 这边是空格
&#033;-->!<--
&#034;-->"<-- xml中需要在前面加入
&#035;-->#<--
&#036;-->$<--
&#037;-->%<--
&#038;-->&<--
&#039;-->'<--
&#040;-->(<--
&#041;-->)<--
&#042;-->*<--
&#043;-->+<--
&#044;-->,<--
&#045;-->-<--
&#046;-->.<--
&#047;-->/<--
&#058;-->:<--
&#059;-->;<--
&#060;--><<--
&#061;-->=<--
&#062;-->><--
&#063;-->?<--
&#064;-->@<--
&#091;-->[<--
&#092;-->--
&#093;-->]<--
&#094;-->^<--
&#095;-->_<--
&#096;-->`<--
&#123;-->{<--
&#124;-->|<--
&#125;-->}<--
&#126;-->~<--
&#160;--> <--这边是空格,在xml首字符中不会被忽略
&#161;-->?<--
&#162;-->¢<--
&#163;-->£<--
&#164;-->¤<--
&#165;-->¥<--
&#166;-->|<--
&#167;-->§<--
&#168;-->¨<--
&#169;-->?<--
&#170;-->a<--
&#171;-->?<--
&#172;-->?<--
&#173;-->-<--
&#174;-->?<--
&#175;-->ˉ<--
&#176;-->°<--
&#177;-->±<--
&#178;-->2<--
&#179;-->3<--
&#180;-->′<--
&#181;-->μ<--
&#182;-->?<--
&#183;-->?<--
&#184;-->?<--
&#185;-->1<--
&#186;-->o<--
&#187;-->?<--
&#188;-->?<--
&#189;-->?<--
&#190;-->?<--
&#191;-->?<--
&#192;-->à<--
&#193;-->á<--
&#194;-->?<--
&#195;-->?<--
&#196;-->?<--
&#197;-->?<--
&#198;-->?<--
&#199;-->?<--
&#200;-->è<--
&#201;-->é<--
&#202;-->ê<--
&#203;-->?<--
&#204;-->ì<--
&#205;-->í<--
&#206;-->?<--
&#207;-->?<--
&#208;-->D<--
&#209;-->?<--
&#210;-->ò<--
&#211;-->ó<--
&#212;-->?<--
&#213;-->?<--
&#214;-->?<--
&#215;-->×<--
&#216;-->?<--
&#217;-->ù<--
&#218;-->ú<--
&#219;-->?<--
&#220;-->ü<--
&#221;-->Y<--
&#222;-->T<--
&#223;-->?<--
&#224;-->à<--
&#225;-->á<--
&#226;-->a<--
&#227;-->?<--
&#228;-->?<--
&#229;-->?<--
&#230;-->?<--
&#231;-->?<--
&#232;-->è<--
&#233;-->é<--
&#234;-->ê<--
&#235;-->?<--
&#236;-->ì<--
&#237;-->í<--
&#238;-->?<--
&#239;-->?<--
&#240;-->e<--
&#241;-->?<--
&#242;-->ò<--
&#243;-->ó<--
&#244;-->?<--
&#245;-->?<--
&#246;-->?<--
&#247;-->÷<--
&#248;-->?<--
&#249;-->ù<--
&#250;-->ú<--
&#251;-->?<--
&#252;-->ü<--
&#253;-->y<--
&#254;-->t<--
&#255;-->?<--
&#256;-->ā<--
&#257;-->ā<--
&#258;-->?<--
&#259;-->?<--
&#260;-->?<--
&#261;-->?<--
&#262;-->?<--
&#263;-->?<--
&#264;-->?<--
&#265;-->?<--
&#266;-->?<--
&#267;-->?<--
&#268;-->?<--
&#269;-->?<--
&#270;-->?<--
&#271;-->?<--
&#272;-->?<--
&#273;-->?<--
&#274;-->ē<--
&#275;-->ē<--
&#276;-->?<--
&#277;-->?<--
&#278;-->?<--
&#279;-->?<--
&#280;-->?<--
&#281;-->?<--
&#282;-->ě<--
&#283;-->ě<--
&#284;-->?<--
&#285;-->?<--
&#286;-->?<--
&#287;-->?<--
&#288;-->?<--
&#289;-->?<--
&#290;-->?<--
&#291;-->?<--
&#292;-->?<--
&#293;-->?<--
&#294;-->?<--
&#295;-->?<--
&#296;-->?<--
&#297;-->?<--
&#298;-->ī<--
&#299;-->ī<--
&#300;-->?<--
&#301;-->?<--
&#302;-->?<--
&#303;-->?<--
&#304;-->?<--
&#305;-->?<--
&#306;-->?<--
&#307;-->?<--
&#308;-->?<--
&#309;-->?<--
&#310;-->?<--
&#311;-->?<--
&#312;-->?<--
&#313;-->?<--
&#314;-->?<--
&#315;-->?<--
&#316;-->?<--
&#317;-->?<--
&#318;-->?<--
&#319;-->?<--
&#320;-->?<--
&#321;-->?<--
&#322;-->?<--
&#323;-->?<--
&#324;-->ń<--
&#325;-->?<--
&#326;-->?<--
&#327;-->?<--
&#328;-->ň<--
&#329;-->?<--
&#330;-->?<--
&#331;-->?<--
&#332;-->ō<--
&#333;-->ō<--
&#334;-->?<--
&#335;-->?<--
&#336;-->?<--
&#337;-->?<--
&#338;-->?<--
&#339;-->?<--
&#340;-->?<--
&#341;-->?<--
&#342;-->?<--
&#343;-->?<--
&#344;-->?<--
&#345;-->?<--
&#346;-->?<--
&#347;-->?<--
&#348;-->?<--
&#349;-->?<--
&#350;-->?<--
&#351;-->?<--
&#352;-->?<--
&#353;-->?<--
&#354;-->?<--
&#355;-->?<--
&#356;-->?<--
&#357;-->?<--
&#358;-->?<--
&#359;-->?<--
&#360;-->?<--
&#361;-->?<--
&#362;-->ū<--
&#363;-->ū<--
&#364;-->?<--
&#365;-->?<--
&#366;-->?<--
&#367;-->?<--
&#368;-->?<--
&#369;-->?<--
&#370;-->?<--
&#371;-->?<--
&#372;-->?<--
&#373;-->?<--
&#374;-->?<--
&#375;-->?<--
&#376;-->?<--
&#377;-->?<--
&#378;-->?<--
&#379;-->?<--
&#380;-->?<--
&#381;-->?<--

&#382;-->?<--

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.xml中的 % 导致编译失败,

这是由于新的SDK采用了新版本的aapt(Android项目编译器),这个版本的aapt编译起来会比老版本更加的严格,然后在Android最新的开发文档的描述String的部分,已经说明了如何去设置 %s 等符号,下面是文档片段:

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.format(String,Object...) 来格式化你的字符串,你可以把格式化参数放在你的字符串中,参见下面的例子:

<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:

在这个例子中,这个格式化的字符串有2个参数, %1$s是个字符串 %2$d 是个浮点数,你可以在你的程序中按照下面的方法来根据参数来格式化字符串:

Resources res = getResources();

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

那么根据例子上说的我需要把%s换成%1$s才行了,修改后编译通过,程序成功启动。

问题补充:

有读者问如何在<string></string>中使用%号

有两个办法可供选择
1. 用%%来表示1个%,和转意符号 / 的用法相同

2.如果你的字符串不需要格式化,可以在你的<string 标签上增加一个属性:formatted="false"例如 <string name="test" formatted="false">% test %</string> 即可。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读