当前位置: 首页 > news >正文

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

相关文章:

  • JDBCRealm Http Digest
  • 开始nodejs+express的学习+实践(1)
  • 计算器,电话号,button输入时如何不会出现后一个替换覆盖前一个
  • Raid5磁盘阵列修复方法介绍
  • 手机自动化测试:Appium源码分析之跟踪代码分析五
  • 解决vsftpd日志时间问题
  • adb install INSTALL_FAILED_ALREADY_EXISTS
  • jQuery 参考手册 - 选择器
  • 数数苹果手机中的不科学
  • Ipsec transport mode and turnnel mode
  • 【探索】无形验证码 —— PoW 算力验证
  • SQL Join的一些总结
  • Cordova 问题点备忘
  • poj 1011 Sticks ,剪枝神题
  • 删除缓存
  • cookie和session
  • CSS 三角实现
  • CSS实用技巧干货
  • emacs初体验
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • Shadow DOM 内部构造及如何构建独立组件
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • vue学习系列(二)vue-cli
  • webpack+react项目初体验——记录我的webpack环境配置
  • 阿里云Kubernetes容器服务上体验Knative
  • 观察者模式实现非直接耦合
  • 利用jquery编写加法运算验证码
  • 每天一个设计模式之命令模式
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 强力优化Rancher k8s中国区的使用体验
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​io --- 处理流的核心工具​
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • (2)STL算法之元素计数
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (ZT)薛涌:谈贫说富
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转) Face-Resources
  • .NET 8.0 发布到 IIS
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .Net FrameWork总结
  • .NET HttpWebRequest、WebClient、HttpClient
  • .net 发送邮件
  • .NET 设计模式—简单工厂(Simple Factory Pattern)
  • .net2005怎么读string形的xml,不是xml文件。
  • .Net7 环境安装配置
  • .Net8 Blazor 尝鲜
  • .NET连接MongoDB数据库实例教程
  • @font-face 用字体画图标