Android:有什么比 android:ellipsize="end" 更好的方法来添加 "..." 到截断的长字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3769105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:03:37  来源:igfitidea点击:

Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?

androidstringlayouttruncateandroid-layout

提问by OneWorld

This propertymakes

这个属性使

"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.

现在我截断了 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);

Update

更新

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

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

12345678901234567890 becomes "12345678901234..."

12345678901234567890 变成“12345678901234...”

However,

然而,

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

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

Update 2

更新 2

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

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

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

这是来自 Motorola Milestone 和 android 2.1 update 1 的屏幕截图。同样的情况在 HTC Desire 上使用相同的 android 版本

Update 3

更新 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.

现在我使用 android:ellipsize="marquee"。这似乎是唯一正常工作的设置。它也只有在聚焦时才会移动。我也在许多其他应用程序中看到它。我猜它的惯例。

采纳答案by OneWorld

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)

请参阅我的更新 3。解决方法是使用“选框”。我已经看到很多应用程序在这个时候这样做。现在,在新版本的 Android 中,此功能很可能已修复,并将按预期工作。(我猜)

回答by bakkay

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.

它对我有用。

回答by Maragues

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

我有一个类似的问题,将此属性设置为 TextView 有帮助:

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.

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

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

也许这不是您问题的解决方案,但它在类似情况下帮助了我。

回答by Josh

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

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

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

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