問題描述
如果 String
比 TextView
的寬度長,它會自動換行到下一行.我可以通過使用 android:singleLine
(已棄用)或設置 android:inputType="text"
來避免這種情況.我現在需要的是用..."替換我的 String
的最后 3 個字符.由于我沒有使用等寬字體,因此這將始終根據我的 String
中使用的字母而有所不同.所以我想知道在 TextView
中獲取字符串的最后 3 個字符并替換它們的最佳方法是什么.也許Android框架中已經實現了一些東西,因為這一定是一個常見的問題.
If a String
is longer than the TextView
's width it automatically wraps onto the next line. I can avoid this by using android:singleLine
(deprecated) or by setting android:inputType="text"
. What I need now is something that replaces the last 3 characters of my String
with "...". Since I'm not using a monospace font this will always be different depending on the letters used in my String
. So I'm wondering what's the best way to get the last 3 characters of a String in a TextView
and replace them. Maybe there's already something implemented in the Android framework, since this must be a common problem.
推薦答案
你應該能夠使用文本視圖的ellipsize"屬性:
You should be able to use the "ellipsize" property of a text view:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_mytext"
android:ellipsize="end"
android:maxLines="1"
/>
您可能還需要對布局應用重力值;我有時會看到沒有它們的自動拉伸"視圖.
You may also need to apply gravity values to the layout too; I have sometimes seen "auto-stretching" views without them.
這篇關于Android:TextView 自動截斷并替換字符串的最后 3 個字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!