动态链接设置在strings.xml中的文本 [英] Dynamically setting links to text in 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 /价值/ 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>
在我的资源本地化的文本/值-ZH-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 它说,这种方法降低了内容重复应该工作,虽然我不知道什么文件夹我应该把意大利为例。难道是在RES /值,它-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>
和再处理的文字是这样的:
and then processing the text like this:
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但经常EX pression途径可能是困难的,因为我期待在支持非拉丁语系语言。我试图把一个自定义的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 );
但是,这也不能工作。
But this didn't work either.
所以,我想知道是否有动态添加多个URL以将链接到网页内容?
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?
推荐答案
尝试使用 Html.fromHtml()
将HTML转换成 Spannable
您放入的TextView
。你所拥有的#1,我期望的TextView
显示HTML源代码,而不是呈现的HTML。
Try using Html.fromHtml()
to convert the HTML into a Spannable
that you put into the TextView
. With what you have in #1, I would expect the TextView
to show the HTML source, not rendered HTML.
这篇关于动态链接设置在strings.xml中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!