前言
在Android布局中進行使用到空格,以便實現文字的對齊。那么在Android中如何表示一個空格呢?
- 空格: (普通的英文半角空格但不換行)
- 窄空格: 
-  (中文全角空格 (一個中文寬度))
-  (半個中文寬度,但兩個空格比一個中文略大)
-  (一個中文寬度,但用起來會比中文字寬一點點)
- \u3000\u3000(首行縮進)
- \u3000(全角空格(中文符號))
- \u0020(半角空格(英文符號))
- …(省略號)
所以如果想要實現文字對齊,那么可以考慮下面的方案:
方案一:一個漢字寬度的空格: 
方案二:一個漢字寬度的空格:   【用兩個空格(  )占一個漢字的寬度時,兩個空格比一個漢字略窄,三個空格(   )比一個漢字略寬】;使用  ‒時候部分機型轉譯后不是空格,而是“-”;而且在不同機型有不同表現。
方案三:一個漢字寬度的空格:  【比一個中文略大】
方案四:一個漢字寬度的空格: 【比一個中文略大】
注意:以上方案是直接寫在布局文件中,如果是寫在strings.xml文件中,則需要使用\u3000、\u0020這一類的,比如:
<string name="info_pwd">\u3000\u3000密碼:</string>
然后在布局文件中通過下面的方式引用
android:text="@string/info_pwd"
至於,為什么在strings.xml文件中使用\u3000代替 ,則是因為Android Studio3.2 + Gradle Plugn 3.2.0 + Gradle4.6環境下, 、 都不起作用了。
效果圖
效果0:未作處理的效果
<?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="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" android:background="#54ff0000" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" android:background="#5400ff00" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電子郵箱:" android:background="#540000ff" android:padding="8dp"/> </LinearLayout>
效果一、使用 
<?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="wrap_content" android:layout_height="wrap_content" android:text=" 用戶名:" android:background="#54ff0000" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="  密碼:" android:background="#5400ff00" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電子郵箱:" android:background="#540000ff" android:padding="8dp"/> </LinearLayout>
效果二、使用   
<?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="wrap_content" android:layout_height="wrap_content" android:text="   用戶名:" android:background="#54ff0000" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="      密碼:" android:background="#5400ff00" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電子郵箱:" android:background="#540000ff" android:padding="8dp"/> </LinearLayout>
效果三、使用  
<?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="wrap_content" android:layout_height="wrap_content" android:text="  用戶名:" android:background="#54ff0000" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="    密碼:" android:background="#5400ff00" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電子郵箱:" android:background="#540000ff" android:padding="8dp"/> </LinearLayout>
效果四、使用 
<?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="wrap_content" android:layout_height="wrap_content" android:text=" 用戶名:" android:background="#54ff0000" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="  密碼:" android:background="#5400ff00" android:padding="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電子郵箱:" android:background="#540000ff" android:padding="8dp"/> </LinearLayout>