Android - 椭圆和截断文本视图中的所有长网址 [英] Android - Ellipsize & Truncate all long Urls in a Textview

查看:21
本文介绍了Android - 椭圆和截断文本视图中的所有长网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 应用的 textview 中,如果 URL 的长度大于某个限制,我想省略所有 URL(已经使用可点击的跨度链接)以获取省略号(或截断).

此行为的灵感来自 twitter 和 facebook.

例如,链接 (截图来源:推特)

我知道这必须涉及跨度.我似乎无法找到允许我在保留链接的同时替换文本的跨度.

解决方案

以下代码将为您解决问题

public class LinkShortener {公共静态最终 int MAX_LINK_LENGTH = 20;公共静态 CharSequence 缩短链接(字符串文本){返回缩短链接(文本,Linkify.ALL);}公共静态 CharSequence 缩短链接(字符串文本,int linkMask){SpannableStringBuilder builder = new SpannableStringBuilder(text);Linkify.addLinks(builder, linkMask);URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class);对于(URLSpan 跨度:跨度){int start = builder.getSpanStart(span);int end = builder.getSpanEnd(span);int flags = builder.getSpanFlags(span);CharSequence linkText = builder.subSequence(start, end);如果 (linkText.length() > MAX_LINK_LENGTH) {linkText = linkText.subSequence(0, 20) + "...";builder.replace(开始,结束,链接文本);builder.removeSpan(span);builder.setSpan(span, start, start+linkText.length(), flags);}}返回生成器;}}

然后你可以简单地像这样使用它:

 itemView.setText(LinkShortener.shortenLinks("https://example.com/really_long_url"));

您可能需要在文本视图上禁用自动链接

代码首先使用 Android 工具中的构建链接文本.然后它遍历所有创建的 URLSpans,并通过替换缩短文本.最后,我们更改跨度以确保它具有正确的边界.由于我们正在重用现有的跨度,因此 URL 将被保留

In my android app's textview, I want to ellipsis all URLs (which already have been linked using clickable span) to get ellipsis (or truncate) if the length of URL is greater than a certain limit.

This behaviour is inspired from twitter and facebook.

For example, the link http://www.getfluttr.com/flap/3rL7/now-only-if-modi-would-listen-to-opposition-party-/ it should look something like this:

(Screenshot Source: Twitter)

I understand that this has to involve spans. I can't seem to be able to find a span that'll allow me to replace text while keeping link.

解决方案

the following code will do the trick for you

public class LinkShortener {

    public static final int MAX_LINK_LENGTH = 20;

    public static CharSequence shortenLinks(String text) {
        return shortenLinks(text, Linkify.ALL);
    }

    public static CharSequence shortenLinks(String text, int linkMask) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        Linkify.addLinks(builder, linkMask);
        URLSpan[] spans = builder.getSpans(0, builder.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = builder.getSpanStart(span);
            int end = builder.getSpanEnd(span);
            int flags = builder.getSpanFlags(span);

            CharSequence linkText = builder.subSequence(start, end);
            if (linkText.length() > MAX_LINK_LENGTH) {
                linkText = linkText.subSequence(0, 20) + "…";
                builder.replace(start, end, linkText);
                builder.removeSpan(span);
                builder.setSpan(span, start, start+linkText.length(), flags);
            }
        }
        return builder;
    }
}

Then you can simply use it like this:

 itemView.setText(LinkShortener.shortenLinks("https://example.com/really_long_url"));

You might need to disable autoLink on the text view

The code first linkifies the text using the build in Android tooling. Then it goes through all creates URLSpans, and shortens the text via replace. Lastly we change the span to make sure it has the right bounds. Since we are reusing the existing span, the URL will be preserved

这篇关于Android - 椭圆和截断文本视图中的所有长网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆