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

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

这里写图片描述

这样的效果,谷歌也没给出这样效果的方法,网上查也没找到实现方法,然后想,中间用空格填充呢?试试,然后写了几个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"
            />

版权声明:本文为fan7983377原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>