在 strings.xml 中动态设置文本链接 [英] Dynamically setting links to text in strings.xml

查看:23
本文介绍了在 strings.xml 中动态设置文本链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个内置本地化的应用程序,但我想要一种可以在文本中创建 Web 链接的方法,该 URL 在其他地方定义(为了便于维护).

I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance).

所以,我在 res/values/strings.xml 中有我的链接:

So, I have my links in res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <string name="link1">http://some.link.com</string>
    <string name="link2">http://some.link2.com</string>
</resources>

以及我在 res/values-en-rGB/strings.xml 中的本地化文本

and my localised text in res/values-en-rGB/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <string name="sampleText">Sample text
More text and link1
More text and link2.</string>
</resources>

我没有测试过这个位,但来自 本地化部分developer.android.com 它说这种减少内容重复的方法应该有效,尽管我不确定我应该将意大利文放在哪个文件夹中.它会在res/values-it-rIT/strings.xml"中吗?假设我也有各种其他语言.

I've not tested this bit, but from the localization section of developer.android.com it says that this approach to reducing content duplication should work, although I'm not sure what folder I should put Italian, for example. Would it be in 'res/values-it-rIT/strings.xml'? Lets assume that I have various other languages too.

我正在寻找一种方法来获取基本本地化的sampleText"并插入我的 html 链接,并在点击时让它们工作.到目前为止,我已经尝试了两种方法:

I'm looking for a way of taking the base localised 'sampleText' and inserting my html links in, and getting them to work when clicked on. I've tried two approaches so far:

1,

在 'sampleText' (%s) 中设置一些格式:

Putting some formatting in the 'sampleText' (%s):

<string name="sampleText">Sample text
More text and <a href="%s">link1</a>
More text and <a href="%s">link2</a>.</string>

然后像这样处理文本:

TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(getResources().getString(R.string.sampleText, getResources().getString(R.string.link1), getResources().getString(R.string.link2)));

但是当我点击链接时这不起作用,即使链接文本被放入正确的位置.

But this didn't work when I click on the link, even though the link text is being put in to the correct places.

2,我尝试使用 Linkify,但正则表达式路线可能很困难,因为我正在考虑支持基于非拉丁语的语言.我尝试在链接文本周围放置一个自定义 xml 标记,然后执行以下操作:

2, I tried to use Linkify but the regular expression route may be difficult as I'm looking at supporting non-Latin based languages. I tried to put a custom xml tag around the link text and then do something like this:

Pattern wordMatcher = Pattern.compile("<span1>.*</span1>");
String viewURL =    "content://" + getResources().getString(R.string.someLink);
Linkify.addLinks(tv, wordMatcher , viewURL );

但这也没有用.

那么,我想知道是否有一种方法可以将多个 URL 动态添加到将链接到 Web 内容的同一文本的不同部分?

So, I'd like to know if there's a way of dynamically adding multiple URLs to different sections of the same text which will link to web content?

推荐答案

问题是你的a href"链接标签在 strings.xml 中,当 strings.xml 被解析为标签.xml 被解析,这是你不想要的.这意味着您需要让它忽略使用 XML 的 CDATA 的标签:

The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:

<string name="sampleText">示例文本<![CDATA[<a href="http://www.google.com">link1</a>]]></string>

然后您可以继续使用 Html.fromHtml() 并使用 LinkMovementMethod 使其可点击:

And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:

TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());

这篇关于在 strings.xml 中动态设置文本链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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