安卓TextView内容过长显示省略号

版权声明:本文为程序园中猿原创文章,转载请注明出处 https://blog.csdn.net/yinxing2008/article/details/84850058

背景

经常会遇到文字过长时,需要在结尾显示省略号,在此总结一下所有的设置方法。
带省略号显示效果图

解决方案(按照推荐级别从高到低排列):

  1. 宽度采用wrap_content,设置maxEms。注意:maxEms不是任意字符的数量,是相当于对应数量大写M宽度。如果输入abc这样的字符,就可以输入超过maxEms的数量值。
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:maxEms="5"
  1. 宽度采用wrap_content,设置maxWidth,超过此值,则显示省略号
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:maxWidth="70dp"
  1. 设置layout_width为固定值。这种方式对比maxWidth的缺点是,无论textview实际由多少内容,都会占用这么大的地方。
android:layout_width="70dp"
android:maxLines="1"
android:ellipsize="end"

附录:

Android中TextView内容过长未显示省略号的问题

安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/84850058