6

我有一个带有 android:autoLink="web" 的 TextView。文本包含一些 URL。

例如:
http ://example.com/

但是在渲染时,它会链接名称方案和域名,但会忽略根路径。

示例呈现为:
http : //example.com/

为什么它会这样做,我如何让它正确地自动链接完全限定的 URL?

编辑:此外,URL 后跟句号或逗号:
http://example.com/

正在呈现为:
http://example.com/,

请注意,StackExchange 自动链接正确(查看此问题的来源)。

编辑:山姆,这是代码:

        <TextView android:id="@+id/open_source"
            android:text="@string/open_source"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFF"
            android:paddingBottom="10dp"
            android:autoLink="web" />

一些文字:

<string name="open_source">Three examples, fully qualified http://isokeys.sourceforge.net/ missing root path http://isokeys.sourceforge.net and followed with a fullstop http://isokeys.sourceforge.net/.</string>

应该呈现为:
三个示例,完全限定的http://isokeys.sourceforge.net/缺少根路径http://isokeys.sourceforge.net并且后跟一个句号http://isokeys.sourceforge.net/

呈现为:
三个示例,完全限定的http://isokeys.sourceforge.net / 缺少根路径http://isokeys.sourceforge.net,然后是句号http://isokeys.sourceforge.net/。

4

1 回答 1

1

我只想指出两件事:
1)当您将 autoLink 设置为等于“web”时,Android 在后台使用 android.text.util 包中的工具来查找包含在您的 View 对象文本中的可操作项。默认情况下,android.text.util.Linkify 对什么是有效的 URL 和无效的 URL 有自己的看法(基于正则表达式匹配)。如果您真的想使用带有斜杠、您或逗号的 URL,您可以使用自己的正则表达式实现您自己的 MatchFilter。可以Android 开发者网站上找到文档
2) 尾部斜杠在标准 URL 中实际上没有任何意义,因为它用于指示资源层次结构中的位置,但如果斜杠后面没有任何内容,那么您不会遍历到另一个级别等级制度。附加逗号或句点不应该是有效的 URL 语法,因为这无助于在您的层次结构中定位资源,所以我假设 Android 的匹配正则表达式会忽略它,以防止您在尝试对链接执行操作时收到 MalformedURLException

于 2012-05-15T20:31:13.207 回答