1. 问题描述
在Android开发中,为一段文本添加空格是常见的需求场景,看似简单,实则大有门道。
我们实现文本有两种方式:
- 引用 string xml 资源文件中的字符串
- 非引用string xml 资源,直接写字符串
对于第1种方式,请参考文章:Android开发如何在String资源中添加空格里面已经做了详细的介绍。
本文主要介绍第2种方式,直接写字符串时如何添加空格。
2. 非引用string xml 资源时添加空格
2.1 直接敲空格
直接能想到的是,手动敲几个空格不就行了?来试试看:⬇️
这里的字符串是 四个空格+测试111
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="20dp"
android:background="@color/green_500">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_1"
android:text=" 测试111"
android:background="@color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_2"
android:text="测试222"
/>
</LinearLayout>
运行效果:
可以看出,确实有空格效果,但看起来应该是英文空格,敲四个空格才相当于一个中文宽度。
这里经过验证,切换中文或英文输入法敲出的空格不影响实际运行效果,都是英文空格。
另外,在Java/kotlin代码使用字符串直接敲空格效果也是一样的。
因此,直接敲空格实现的效果是:英文空格。
2.2 使用Unicode
由前文 Android开发如何在String资源中添加空格可知使用 Unicode 字符可以表示空格,
- 中文空格:
\u3000
(一个中文宽度) - 英文空格:
\u0020
(一个英文字母宽度)
代码示例⬇️,这里的字符串还是 四个空格+测试111
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="20dp"
android:background="@color/green_500">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_1"
android:text="\u3000\u3000\u3000\u3000测试111"
android:background="@color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_2"
android:text="测试测试222"
/>
</LinearLayout>
运行效果:
可见,每个 unicode 字符 \u3000
正确的表示了一个中文空格,同理 \u0020
可以正确表示一个英文空格。
经过验证,在 Java/kotlin 代码中使用字符串也可以使用 unicode 字符实现空格。
2.3 使用 HTML字符实体
在 非 string xml 资源的情况下,还可以使用 HTML 字符实体来表示一些符号,包括空格。
HTML 字符实体:定义
- 中文空格:
 
(一个中文宽度)、 
(半个中文空格) - 英文空格:
 
代码示例⬇️,这里的字符串还是 四个空格+测试111
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="20dp"
android:background="@color/green_500">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_1"
android:gravity="start"
android:text="    测试111"
android:background="@color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_2"
android:gravity="start"
android:text="测试测试222"
/>
</LinearLayout>
运行效果:
- 中文空格:
 
(一个中文宽度)
- 中文空格:
 
(半个中文空格)
- 英文空格:
 
3. 总结
在Android开发中,非引用 string xml 资源,即直接写字符串的情况下,进行添加空格有三种方式:
- 直接敲空格
只能添加英文空格 - 使用 Unicode 字符
可以实现中文和英文空格的添加- 中文空格:
\u3000
(一个中文宽度) - 英文空格:
\u0020
(一个英文字母宽度)
- 中文空格:
- 使用HTML字符实体
可以实现中文和英文空格的添加,且能够添加半个中文宽度的空格- 中文空格:
 
(一个中文宽度)、 
(半个中文空格) - 英文空格:
 
- 中文空格:
以上就是本文的全部内容 🎉,希望这篇文章对你有用,欢迎支持哦~感谢!