Android TextView两端对齐、文本两端对齐

本文介绍了一种在Android应用中实现类似表格排版效果的方法。通过使用特定的占字符(如 、 等),可以有效地调整文本间距,达到接近表格布局的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




如题,在开发中,经常会用到比如说类似于表格的排版,效果图:


这里写图片描述



这样的效果,谷歌也没给出这样效果的方法,网上查也没找到实现方法,然后想,中间用空格填充呢?试试,然后写了几个TextView对比一下,如下:


这里写图片描述




放大效果:


这里写图片描述




可以看出,不能完美对齐,这肯定不是想要的效果,然后就发现了两个占字符来实现:

 
 
 
 



他们与汉字换算关系就是1个汉字 = 4个  = 4个  = 1个  = 2个 


如下:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是测试:"
            android:textSize="22sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;码:"
            android:textSize="22sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密&#12288;&#12288;码:"
            android:textSize="22sp"
            />



效果图:


这里写图片描述



放大效果:

这里写图片描述




至于两个怎么用,就看需求了,比如这个:


这里写图片描述


使用:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是测试:"
            android:textSize="22sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小&#160;&#160;密&#160;&#160;码:"
            android:textSize="22sp"
            />
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值