0%

Android XML字符串

Android在开发过程中,一些特殊字符时无法直接在 strings.xml 文件中写,需要用对应的转义字符代替或者在特殊符号(比如:´" 等待)前添加 \ ,比如一个 TextView 控件中,需要动态替换其中的一些数据,再比如需要调整 TextView 字体的一些HTML样式(比如:粗体,斜体,下划线等),虽然这些都可以用 TextView 去修改,但更简单的方法是设置string提供的属性即可

特殊字符

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- 无法直接使用I'm developer
<string name="name_introduce">I'm developer</string> -->
<!-- 解决方法一 转义字符代替 -->
<string name="name_introduce">I&#039;m developer</string>
<!-- 解决方法二 使用 \ -->
<string name="name_introduces">I\'m developer</string>
</resources>

动态替换或拼接

  • %n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格
  • %n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格
  • %n$mf:代表输出的是浮点数,n代表第几个参数,m在浮点类型之前放置几个空格

XML配置

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
%1$s 第一个参数,对应Jerry
%2$d 第二个参数,对应36
%3$4.2f 第三个参数,对应 195.1255 ,但保留两位小数,实际显示195.13
-->
<string name="welcome_messages">Hello, %1$s, You have %2$d new messages. total cost %3$4.2f</string>
</resources>

Java设置

1
2
mTextConent = (TextView) findViewById(R.id.tv_String);
mTextConent.setText(String.format(getString(R.string.welcome_messages), "Jerry", 36, 195.1255));

HTML标记

  • <b> 表示 粗体 文本。
  • <i> 表示 斜体 文本。
  • <u> 表示 下划线 文本。
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
<string name="android">Welcome to <i>Android</i>!</string>
<string name="hello">Welcome to <u>Android</u>!</string>
</resources>

ASCII对照表

ASCII码 符号 ASCII码 符号 ASCII码 符号 ASCII码 符号
&#064; @ &#058; : &#160; 空格 &#032; 空格
&#033; ! &#034; " &#035; # &#036; $
&#037; % &#038; & &#039; ´ &#040; (
&#042; * &#043; + &#044; , &#041; )
&#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; ª &#171; «
&#172; ¬ &#173; &#174; ® &#175; ¯
&#176; ° &#177; ± &#178; ² &#179; ³
&#180; ´ &#181; µ &#182; &#183;
&#184; ¸ &#185; ¹ &#186; º &#187; »
&#188; ¼ &#189; ½ &#190; ¾ &#191; ¿
&#192; À &#193; Á &#194; Â &#195; Ã
&#196; Ä &#197; Å &#198; Æ &#199; Ç
&#200; È &#201; É &#202; Ê &#203; Ë
&#204; Ì &#205; Í &#206; Î &#207; Ï
&#208; Ð &#209; Ñ &#210; Ò &#211; Ó
&#212; Ô &#213; Õ &#214; Ö &#215; ×
&#216; Ø &#217; Ù &#218; Ú &#219; Û
&#220; Ü &#221; Ý &#222; Þ &#223; ß
&#224; à &#225; á &#226; â &#227; ã
&#228; ä &#229; å &#230; æ &#231; ç
&#232; è &#233; é &#234; ê &#235; ë
&#236; ì &#237; í &#238; î &#239; ï
&#240; ð &#241; ñ &#242; ò &#243; ó
&#244; ô &#245; õ &#246; ö &#247; ÷
&#248; ø &#249; ù &#250; ú &#251; û
&#252; ü &#253; ý &#254; þ &#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; IJ &#307; ij
&#308; Ĵ &#309; ĵ &#310; Ķ &#311; ķ
&#312; ĸ &#313; Ĺ &#314; ĺ &#315; Ļ
&#316; ļ &#317; Ľ &#318; ľ &#319; Ŀ
&#320; ŀ &#321; Ł &#322; ł &#323; Ń
&#324; ń &#325; Ņ &#326; ņ &#327; Ň
&#328; ň &#329; ʼn &#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; ž

附录

Jerry xu wechat
一个程序猿的成长历程~