简体   繁体   中英

cut off text if it doesn't fit in the Textview widget in Android?

I have a TextView and let's say for example I set it's width to 100dp. If the text value is too long, it automatically tries to make a new line.

I DON'T want to do that. I want to just show "..." at the end of the text so the user know there is more to this text. When the user clicks on it, they will be able to all the text.

How can I limit it so the text doesn't go to the next line and always stays with the TextView's fixed size?

Thank you for your time.

您可以使用以下android:singleLine="true"

I always use a small set of attributes here, in order to make it compatible to many devices:

<TextView 
  android:id="@+id/my_text" 
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:text="text"
  android:singleLine="true"
  android:lines="1"
  android:maxLines="1"
  android:ellipsize="end" />

So basically singleLine . lines and maxLines .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM