[轉(zhuǎn)]Android布局中的空格以及占一個漢字寬度的空格,實(shí)現(xiàn)不同漢字字?jǐn)?shù)對齊

轉(zhuǎn)自:Android布局中的空格以及占一個漢字寬度的空格,實(shí)現(xiàn)不同漢字字?jǐn)?shù)對齊

前言

在Android布局中進(jìn)行使用到空格,以便實(shí)現(xiàn)文字的對齊。那么在Android中如何表示一個空格呢?

  • 空格: (普通的英文半角空格但不換行)
  • 窄空格: 
  •  (中文全角空格 (一個中文寬度))
  •  (半個中文寬度,但兩個空格比一個中文略大)
  •  (一個中文寬度,但用起來會比中文字寬一點(diǎn)點(diǎn))
  • \u3000\u3000(首行縮進(jìn))
  • \u3000(全角空格(中文符號))
  • \u0020(半角空格(英文符號))
  • …(省略號)

所以如果想要實(shí)現(xiàn)文字對齊,那么可以考慮下面的方案:

  1. 方案一:一個漢字寬度的空格: 

  2. 方案二:一個漢字寬度的空格:   【用兩個空格(  )占一個漢字的寬度時(shí),兩個空格比一個漢字略窄,三個空格(   )比一個漢字略寬】;使用  ‒時(shí)候部分機(jī)型轉(zhuǎn)譯后不是空格,而是“-”;而且在不同機(jī)型有不同表現(xiàn)。

  3. 方案三:一個漢字寬度的空格:  【比一個中文略大】

  4. 方案四:一個漢字寬度的空格: 【比一個中文略大】

注意:以上方案是直接寫在布局文件中,如果是寫在strings.xml文件中,則需要使用\u3000、\u0020這一類的,比如:

<string name="info_pwd">\u3000\u3000密碼:</string>

然后在布局文件中通過下面的方式引用

android:text="@string/info_pwd"

至于,為什么在strings.xml文件中使用\u3000代替&#12288,則是因?yàn)锳ndroid Studio3.2 + Gradle Plugn 3.2.0 + Gradle4.6環(huán)境下,&#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>
img

效果二、使用&#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>
img

效果三、使用&#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>
img

效果四、使用&#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>
img
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容