TextView maxWidth maxLength maxEms 区别

  以下引用网上的介绍。
maxWidth=”80dp” 限制TextView最大宽度。必须与layout_width=”wrap_content”搭配使用,当指定layout_width为其他值时,maxWidth会失效。


maxLength=”10” 限制TextView最多10个字符数。汉字、英文、数字都算一个字符。maxLength属性会使ellipsize=”end”属性失效。添加了maxLength不再会显示”…”


maxEms=”5” 限制TextView的最大宽度为5个大写M的字符宽度。em是一个印刷排版的单位,表示字宽的单位。 em字面意思为:equal M(和M字符一致的宽度为一个单位)简称em。ems是em的复数表达。


     
 引自http://blog.csdn.net/JavaLive09/article/details/38661773
maxEms与maxWidth很相似,只是单位不一样,都是限制TextView的最大宽度,所以可以和ellipsize属性共用。
注意:如果要让TextView 显示几个具体字符。多余的部分显示省略号的话。要maxEms 和ellipsize 共用。 maxLength 使用起来无效
下面看下代码使用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我的名字叫做孙磊我的名字叫做孙磊孙烈"
        android:gravity="center"
        android:maxLength="6"
        android:layout_marginTop="20dp"
        android:ellipsize="end"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的名字叫做孙磊2"
        android:gravity="center"
        android:maxLength="4"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的名字叫做孙磊"
        android:gravity="center"
        android:maxEms="4"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我的名字叫做孙磊"
        android:gravity="center"
        android:maxEms="4"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的名字叫做孙磊"
        android:gravity="center"
        android:maxEms="4"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        />
</LinearLayout>

代码像上面一样。很好使用。
注意:如果 TextView 设置 为宽度 match _parent .maxEms 效果无效的




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值