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)表示的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值