仅椭圆化 TextView 中的一部分
我想知道是否可以仅缩写 TextView 中字符串的一部分。我想做的是这样的:
Element with short title (X)
Element with a very lo...(X)
标题应该是省略号,但 X 必须始终可见。就我而言,不可能使用多个 TextView。您认为有一种简单的方法可以做到这一点吗?
谢谢!
I was wondering if it is possible to abbreviate only a portion of a string in a TextView. What I would like to do is something like this:
Element with short title (X)
Element with a very lo...(X)
The title should be ellipsized, but the X must be always visible. In my case, is not possible to use more than one TextView. Do you think there is a simple way of doing this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实需要一个干净的项目解决方案,所以在四处寻找并没有找到任何我喜欢的解决方案后,我花了一些时间来写这篇文章。
这是具有增强省略号控制的 TextView 的实现。它的工作方式是使用 Android 的 Spanned 界面。它定义了一个枚举,您可以使用它来标记您希望在需要时省略的特定文本部分。
限制:
下面是一个用法示例:
FooActivity.java
res/layouts/foo_layout.xml
就是这样
下面是结果示例:
实现
I really needed a clean solution for a project so after searching around and not finding any solutions I felt I liked, I took some time to write this up.
Here is an implementation of a TextView with enhanced ellipsis control. The way it works is by using Android's Spanned interface. It defines an enum you can use to tag the specific section of text you'd like to be ellipsized if needed.
Limitations:
Here's a sample of the usage:
FooActivity.java
res/layouts/foo_layout.xml
That's it
Here's an example of the result:
The Implementation
你可以尝试使用这样的东西:
它可能不会给你你想要的东西,但它可能会做这样的事情:
参考信息
TruncateAt
setEllipsize
You can try using something like this:
It might not give you exactly what you want though, it may do something like this:
Reference Info
TruncateAt
setEllipsize