关于 TextView 或者 EidtView 。。。。 显示文字如果超长,
怎么让最后的地方 ... 显示出来,还要适应不同尺寸的屏幕(最好还能适应横竖屏切换)
当时为了 先赶工,随便写了个 方式(自己先鄙视一下我自己)
String show = notice.getText();
show = show.length() > 18 ? show.substring(0, 18) + "..." : show;
这个方式漏洞多的 让人抓狂,首先 如果中英文混杂 这里 就没法判断 字符长度了
更别说 不同尺寸的屏幕 和 适应横竖屏切换 了
好了 ,网上看到 一哥么写的 winCE 的文章,
微软都 实现了 GetTextExtentPoint 的方法了,没理由 后起之秀 android
不做吧,
果然,看了 api google 不但做了,而且做的更好,
看到 EditText 里有个 setEllipsize 方法
public void setEllipsize (TextUtils.TruncateAt where)
方法参数 TextUrils.TruncateAt :
看到了吧,不但提供 后面加 ... 前面,中间 也可以,~~~
Summary
Enum Values
TextUtils.TruncateAt END
TextUtils.TruncateAt MARQUEE
TextUtils.TruncateAt MIDDLE
TextUtils.TruncateAt START
哦耶,搞定。
——————————
可能你要问 如果 layout 的 xml 文件里 怎么 弄呢?
public static final int ellipsize
Where to ellipsize text.
Must be one of the following constant values.
| Constant | Value | Description |
|---|---|---|
none | 0 | |
start | 1 | |
middle | 2 | |
end | 3 | |
marquee | 4 |
Constant Value: 16842923 (0x010100ab)
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:singleLine="true"
android:ellipsize="end"
>
</TextView>
这里 要强调两点 一定要注意,不然 你就无缘看到 谷歌给你 加上的 ... 了
android:layout_width="wrap_content" 这个是 必须 的
android:singleLine="true" 这个不必须, 因为 系统可能会两行 然后 加...
本文介绍如何在Android中使用TextView或EditText显示过长的文字时进行截断,并在末尾添加省略号(...), 同时确保内容能适应不同屏幕尺寸及横竖屏切换。
689

被折叠的 条评论
为什么被折叠?



