maxEms和maxLength的区别

2018-05-18  本文已影响65人  __素颜__

前言

我们经常接到设计的要求一行展示多少字,或者展示多宽,最大长度多少等等,,我们经常使用 maxlength 和maxEms,但是maxEm到底什么意思和maxlength的区别还是要仔细搞明白

一.maxlength

maxlength限制输入的文本个数,无论我们输入什么内容,英文,符号,数字,汉字这些都属于文本范围,,

例如

<TextView
        android:gravity="center"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="一二三,四五六,七八九十"
        android:maxLength="10"
        android:textSize="15sp" />

展示效果:

image.png

8个汉字+2个标点=10

一.maxEms

官方解释:textview的宽度不少于很多ems的宽度

此时超级不理解ems是啥?,上网查了下ems是啥,查了很多资料说啥的都有,终于找到靠谱的答案
原来
em是一个印刷排版的单位,表示字宽的单位。 em字面意思为:equal M (和M字符一致的宽度为一个单位)简称em。

ems是em的复数表达。

写个例子

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">
        <TextView
            android:layout_centerInParent="true"
            android:id="@+id/tv_m"
            android:text="MMMM"
            android:textSize="14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/tv_m"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxEms="4"
            android:text="12,345你好"
            android:textSize="14sp" />


    </RelativeLayout>

显示


image.png

第二个textView中设置maxEms=4也就是一行限制和4个M相等的宽度 ,而 "12,345你好" 中 "12,345" 恰好和4个M的宽度相等,所以在5后发生了折行

总结

maxlength :控制一行文本个数
maxEms:控制一行的宽度为M个

上一篇下一篇

猜你喜欢

热点阅读