我的布局中有一个TextView,它是layout_width中的wrap_content.它最多限制为15个字符,所以我使用的是maxLength.
我需要用3个点(...)结束这个TextView,只有当我用dp给layout_width一个固定的大小时才会发生这种情况,这是我不想做的事情.
我知道有可能通过代码并在第15个字符后剪切字符串然后添加3个点,但我更喜欢用XML来做.
知道如何用3个点结束文本并留下wrap_content吗?
<TextView
android:id="@+id/inbox_contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
android:maxLength="15"
android:textColor="#0670b4"
android:textSize="16sp" />
Run Code Online (Sandbox Code Playgroud) 我有一个 TextView,最多 15 个字符,最后它应该有三个点。使用我的 XML,它减少了 15 个字符,但最后没有添加三个点...
。
重要提示:我知道当我用行数限制文本时它会起作用 -
android:maxLines="2"
。但我需要用最大字符数来限制它,如问题所述。
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:maxLength="15"
android:ellipsize="end"
tools:text="123456789qwertyuiopasdfghjklzxcvbnm"
android:textSize="50sp"
/>
Run Code Online (Sandbox Code Playgroud)
结果: