TextView: Get it to truncate without respect to spaces between words

こ雲淡風輕ζ 提交于 2020-01-02 02:38:09

问题


How do you configure a TextView to truncate in the middle of a word?

So if I have text="Some Text" I want it to show as "Some Te" assuming the width supports that.

What instead I'm seeing is "Some"; its truncating the entire word "Text" even though there is plenty of space for a couple more characters.


回答1:


Here is what worked for me:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    android:ellipsize="marquee"/>


Or with "..." at the end:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    />


While using android:lines doesn't work:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:lines="1"
    android:ellipsize="marquee"/>

This is most likely a bug and I believe I've seen some bug report about that.




回答2:


Here is my code for a TextView that "truncates" the word with no ellipsis. It doesn't quite cut it off, it simply lets it run off the edge, giving the slight impression that it's been truncated.

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:scrollHorizontally="false"
    android:ellipsize="none"
    android:text="@string/hello"
/>



回答3:


Found Two ways to Truncating texview.
Text to truncate : "Hello World! sample_text_truncate"
Case 1:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"`enter code here`
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:singleLine="true"/>

Result: Hello World! sample_text_trunc...

Case 2:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:maxLines="1"/>

Result: Hello World!...

So i found difference in singleline and maxlines properties.Unfortunately singleline in depricated.I wanted singleline result.


来源:https://stackoverflow.com/questions/6600897/textview-get-it-to-truncate-without-respect-to-spaces-between-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!