寫(xiě)過(guò)html代碼的朋友們應(yīng)該知道,html支持轉(zhuǎn)義字符,可以通過(guò)轉(zhuǎn)義字符實(shí)現(xiàn)顯示空格、縮進(jìn)、換行等內(nèi)容。在Android開(kāi)發(fā)中,我們同樣可以通過(guò)轉(zhuǎn)義字符,實(shí)現(xiàn)特殊字符的顯示。
1. 轉(zhuǎn)義字符的顯示形式
Android的轉(zhuǎn)義字符通過(guò)unicode編碼來(lái)表示。常用的顯示形式有Hex形式和HTML形式。
常見(jiàn)的轉(zhuǎn)義字符如下:
| 字符 | HTML | Hex |
|---|---|---|
| 半角空格(半角符號(hào)) |   | \u0020 |
| 全角空格(中文符號(hào)) |   | \u3000 |
| 首行縮進(jìn) |    | \u3000\u3000 |
| & | & | \u0026 |
| @ | @ | \u0040 |
| % | &37; | \u25 |
在布局文件中使用轉(zhuǎn)義字符,需要使用HTML形式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓  名" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="電話號(hào)碼" />
</LinearLayout>
而在string.xml中,則需要使用Hex形式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name">姓\(chéng)u3000\u3000名</string>
<string name="cellphone">電話號(hào)碼</string>
</resources>
2. 顯示形式的換算
HTML形式實(shí)際上是將Unicode編碼以十進(jìn)制的形式顯示,而Hex形式實(shí)際上是將Unicode編碼以十六進(jìn)制的形式顯示,因此顯示形式的換算本質(zhì)上是不同進(jìn)制的換算。
如@中的數(shù)字為64換算成十六進(jìn)制的結(jié)果為40,對(duì)應(yīng)著\u0040中的數(shù)字40,下圖是計(jì)算器的顯示結(jié)果:

計(jì)算結(jié)果.png
最后,貼出Unicode編碼查詢網(wǎng)站,方便各位查詢Unicode編碼:
Unicode Lookup
FileFormat.Info
參考博客:
字符編碼筆記:ASCII,Unicode 和 UTF-8