TextView: Get it to truncate without respect to sp

2019-04-29 02:11发布

问题:

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.