TextView限制长度,显示省略号

  1. 首先TextView的android:layout_width属性必须设置为wrap_content,如果设置match_parent则不起作用因为TextView的长度已经超过了显示的字符串的长度
  2. 设置maxEms属性限制显示的长度,注意:不是设置maxLength属性 ,设置android:ellipsize属性设置省略号的显示位置
  3. 设置限制单行显示, 通过android:lines="1"属性或android:maxLines="1"都可以
  • 以上属性都设置后就能实现想要的效果了,
  <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="@dimen/dp_15"
        android:layout_marginRight="@dimen/dp_15"
        android:maxEms="5"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"
        android:text="此处是限制字符长度,显示省略号的示例"/>

猜你喜欢

转载自blog.csdn.net/xiaopihair123/article/details/129851092