Android布局中的空格以及占一個漢字寬度的空格,實現不同漢字字數對齊


前言

在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代替&#12288,則是因為Android Studio3.2 + Gradle Plugn 3.2.0 + Gradle4.6環境下,&#12288;、&#160;都不起作用了。

效果圖

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

效果一、使用&#12288;

<?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="&#12288;用戶名:"
        android:background="#54ff0000"
        android:padding="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&#12288;&#12288;密碼:"
        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>

效果二、使用&#160;&#160;&#8201;

<?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="&#160;&#160;&#8201;用戶名:"
        android:background="#54ff0000"
        android:padding="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&#160;&#160;&#8201;&#160;&#160;&#8201;密碼:"
        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>

效果三、使用&#8194;&#8194;

<?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="&#8194;&#8194;用戶名:"
        android:background="#54ff0000"
        android:padding="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&#8194;&#8194;&#8194;&#8194;密碼:"
        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>

效果四、使用&#8195;

<?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="&#8195;用戶名:"
        android:background="#54ff0000"
        android:padding="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&#8195;&#8195;密碼:"
        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>

參考資料:

Android不同漢字字數對齊

html 空白漢字占位符&#12288;

Android string.xml中使用html標簽

Android開發中Html.fromHtml(String source)方法過時的替代方法

三種空格unicode(\u00A0,\u0020,\u3000)表示的區別


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM