Android 动态设置指向strings.xml中文本的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2244194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically setting links to text in strings.xml
提问by Martyn
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).
我正在尝试制作一个内置本地化的应用程序,但我想要一种可以在文本中创建 Web 链接的方法,URL 在其他地方定义(为了便于维护)。
So, I have my links in res/values/strings.xml:
所以,我在 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>
and my localised text in res/values-en-rGB/strings.xml
和我在 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>
I've not tested this bit, but from the localization section of developer.android.comit 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.
我还没有测试过这一点,但是从developer.android.com的本地化部分它说这种减少内容重复的方法应该有效,尽管我不确定我应该把意大利语放在哪个文件夹中。它会在“res/values-it-rIT/strings.xml”中吗?让我们假设我也有各种其他语言。
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:
我正在寻找一种方法来获取基本本地化的“sampleText”并插入我的 html 链接,并在单击时让它们工作。到目前为止,我已经尝试了两种方法:
1,
1、
Putting some formatting in the 'sampleText' (%s):
在 '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, 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:
2,我尝试使用 Linkify,但正则表达式路线可能很困难,因为我正在考虑支持非拉丁语系。我尝试在链接文本周围放置一个自定义 xml 标记,然后执行以下操作:
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.
但这也不起作用。
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?
所以,我想知道是否有一种方法可以将多个 URL 动态添加到将链接到 Web 内容的同一文本的不同部分?
回答by dule
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:
问题是您的“a href”链接标签在strings.xml
解析时被解析为标签 strings.xml
,这是您不想要的。这意味着您需要使用 XML 的 CDATA 忽略标签:
<string name="sampleText">Sample text <![CDATA[<a href="http://www.google.com">link1</a>]]></string>
<string name="sampleText">Sample text <![CDATA[<a href="http://www.google.com">link1</a>]]></string>
And then you can continue with Html.fromHtml()
and make it clickable with LinkMovementMethod
:
然后您可以继续Html.fromHtml()
并使用以下命令使其可点击LinkMovementMethod
:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
回答by ozbek
In your layout set android:autoLink
to web
在您的布局中设置android:autoLink
为web
<TextView android:text="@string/text_with_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
And in strings.xml just add the URL(s).
在 strings.xml 中只需添加 URL(s)。
<string name="text_with_url">http://stackoverflow.com/ FTW!</string>
回答by CommonsWare
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.
尝试使用Html.fromHtml()
将 HTML 转换为Spannable
您放入TextView
. 使用 #1 中的内容,我希望TextView
显示 HTML 源代码,而不是呈现的 HTML。
回答by dhesse
You have to implement
你必须实施
setMovementMethod(LinkMovementMethod.getInstance());
on your Textview
在你的文本视图上
Here is a better example:
这是一个更好的例子: