Android:比 android:ellipsize="end" 更好的东西添加“...”截断长字符串?

发布于 2024-09-24 10:51:15 字数 1325 浏览 7 评论 0 原文

此属性使得

“短词和长词”

“短和”

。但我想要拥有某物。喜欢

“短而长……”

现在我在Java代码中截断字符串。但是,这是基于字符数而不是链接的实际长度。所以,结果并不是很好。

String title;
    if(model.getOrganization().length() > 19) {
      title = model.getText().substring(0, 15).trim() + "…";
    } else {
      title = model.getText();
    }
    ((TextView) findViewById(R.id.TextViewTitle)).setText(title);

更新

刚刚注意到,这个属性实际上在少数情况下添加了“...”。但并非所有这些:

12345678901234567890 变为 “12345678901234...”

然而,

“1234567890 1234567890” 变成“1234567890”而不是 “1234567890 123...”

更新 2

现在真的变得很时髦!我只是设置了 singleLine=true 并删除了 maxLine (无论是否设置 ellipsize 属性都会出现该错误)...

alt text

这个是来自 Motorola Milestone 的 android 2.1 update 1 的屏幕截图。同样的情况发生在具有相同 android 版本

Update 3

的 HTC Desire 上。现在我使用 android:ellipsize="marquee"。这似乎是唯一正常工作的设置。它也只有在集中注意力时才会移动。我也在许多其他应用程序中看到它。我想这是常见的做法。

This property makes

"short and very-long-word"

to

"short and"

. But I want to have smth. like

"short and very-lon..."

Right now I truncate the String in Java code. However, thats based on the number of characters and not the actual length of the link. So, the result isn't very nice.

String title;
    if(model.getOrganization().length() > 19) {
      title = model.getText().substring(0, 15).trim() + "…";
    } else {
      title = model.getText();
    }
    ((TextView) findViewById(R.id.TextViewTitle)).setText(title);

Update

Just noticed, this property actually adds "..." in a few cases. But not in all of them:

12345678901234567890 becomes
"12345678901234..."

However,

"1234567890 1234567890"
becomes "1234567890" and not
"1234567890 123..."

Update 2

Now it really gets funky! I just set singleLine=true and removed maxLine (The bug appears with and without setting ellipsize attribute)...

alt text

This is a screenshot take from Motorola Milestone with android 2.1 update 1. Same happens on HTC Desire with the same android version

Update 3

Now I use android:ellipsize="marquee". That seems to be the only properly working setting. It's also only moving, when focused. I see it in many other apps also. I guess its common practise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2024-10-01 10:51:15

如果是单行,请尝试以下操作:

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

它对我有用。

If it's for a single line try this:

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

It worked for me.

む无字情书 2024-10-01 10:51:15

我遇到了类似的问题,并将此属性设置为 TextView 有所帮助:

android:singleLine="true"

另外,我使用的是relativelayout,因此我通过为 TextView 右侧的按钮设置左边距来限制 TextView 可以占用的空间,这样文本就无法显示进一步扩展并被迫截断其内容。

也许这不能解决你的问题,但它在类似的情况下帮助了我。

I had a similar problem and setting this property to the TextView helped:

android:singleLine="true"

Also, I was using a RelativeLayout so I limited the space the TextView could take by setting a left margin to a button to the right of the TextView, so that the text cannot expand further and is forced to truncate its content.

Maybe it's not the solution to your problem, but it helped me in a similar situation.

凉月流沐 2024-10-01 10:51:15

请参阅我的更新 3。解决方法是使用“marquee”。我现在看到很多应用程序都这样做。
现在,随着 Android 的新版本,此功能很可能已得到修复,并且将按预期工作。 (我的猜测)

See my update 3. The workaround was using " marquee". I have seen many apps doing that at this time.
Now, with new versions of Android this feature is most likely fixed and will work as expected. (my guess)

£冰雨忧蓝° 2024-10-01 10:51:15

也许你可以看看 操作系统使用的私有 Ellipsizer 类 可以工作并为您的应用程序修改它吗?

编辑:这是一个工作链接 - http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/text/Layout.java#1830

Maybe you could take a look at how the private Ellipsizer class used by the OS works and modify it for your app?

Edit: Here's a working link - http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/text/Layout.java#1830

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文