Android 常用转义字符

作者: android 发布时间: 2019-08-20 浏览: 4286 次 编辑

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="姓&#12288;&#12288;名" />

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