问题描述
我的布局中有一个TextView,它是layout_width中的wrap_content.最多限制为15个字符,因此我使用的是maxLength.
I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.
我需要以3个点(...)结束此TextView,并且只有在我给dp赋予layout_width固定大小时才会发生这种情况,这是我不想做的事情.
I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size with dp, something that I don't want to do.
我知道可以通过代码做到这一点,并在第15个字符后剪切字符串,然后添加3个点,但是我更喜欢通过XML来实现.
I know it is possible to this by code and cut the string after the 15th character and then add the 3 dots, but I prefer to do that by XML.
有什么主意如何以3个点结束文本并保留wrap_content吗?
Any idea how to end the text with 3 dots and leave it 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" />
推荐答案
这将解决您的问题,在XML代码中使用ellipsize
属性
This will solve your problem, Use ellipsize
Property in your XML Code
android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->
编辑:singleLine
已被弃用.改为使用maxlines="1"
.
这篇关于使用maxLength使用3个点结束TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!