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

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

问题描述

我正在尝试制作一个内置本地化应用程序,但是我想要一种方法,可以在文本中创建一个网页链接,这个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\nMore text and link1\nMore text and link2.</string>
</resources>

我没有测试过这一点,但是从本地化部分的developer.android.com 它说,减少内容重复的这种方法应该工作,虽然我' m不知道我应该把意大利语放在哪个文件夹中。是否在'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\nMore text and <a href="%s">link1</a>\nMore 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 中,并在字符串时被解析为标签。 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天全站免登陆