安卓TextView內容過長顯示省略號
阿新 • • 發佈:2018-12-13
背景
經常會遇到文字過長時,需要在結尾顯示省略號,在此總結一下所有的設定方法。
解決方案(按照推薦級別從高到低排列):
- 寬度採用wrap_content,設定maxEms。注意:maxEms不是任意字元的數量,是相當於對應數量大寫M寬度。如果輸入abc這樣的字元,就可以輸入超過maxEms的數量值。
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:maxEms="5"
- 寬度採用wrap_content,設定maxWidth,超過此值,則顯示省略號
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:maxWidth="70dp"
- 設定layout_width為固定值。這種方式對比maxWidth的缺點是,無論textview實際由多少內容,都會佔用這麼大的地方。
android:layout_width="70dp"
android:maxLines="1"
android:ellipsize="end"
附錄:
安卓開發技術分享: https://blog.csdn.net/yinxing2008/article/details/84555061