寫過html代碼的朋友們應該知道川慌,html支持轉義字符,可以通過轉義字符實現(xiàn)顯示空格祠乃、縮進梦重、換行等內容。在Android開發(fā)中亮瓷,我們同樣可以通過轉義字符琴拧,實現(xiàn)特殊字符的顯示。
1. 轉義字符的顯示形式
Android的轉義字符通過unicode編碼來表示嘱支。常用的顯示形式有Hex
形式和HTML
形式蚓胸。
常見的轉義字符如下:
字符 | HTML | Hex |
---|---|---|
半角空格(半角符號) |   | \u0020 |
全角空格(中文符號) |   | \u3000 |
首行縮進 |    | \u3000\u3000 |
& | & | \u0026 |
@ | @ | \u0040 |
% | &37; | \u25 |
在布局文件中使用轉義字符挣饥,需要使用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="電話號碼" />
</LinearLayout>
而在string.xml中,則需要使用Hex
形式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name">姓\u3000\u3000名</string>
<string name="cellphone">電話號碼</string>
</resources>
2. 顯示形式的換算
HTML
形式實際上是將Unicode編碼以十進制的形式顯示沛膳,而Hex
形式實際上是將Unicode編碼以十六進制的形式顯示扔枫,因此顯示形式的換算本質上是不同進制的換算。
如@
中的數(shù)字為64換算成十六進制的結果為40锹安,對應著\u0040
中的數(shù)字40短荐,下圖是計算器的顯示結果:
計算結果.png
最后,貼出Unicode編碼查詢網(wǎng)站叹哭,方便各位查詢Unicode編碼:
Unicode Lookup
FileFormat.Info
參考博客:
字符編碼筆記:ASCII忍宋,Unicode 和 UTF-8