如何在字符串资源中创建链接(以及其中的其他文本)?- 安卓 [英] How to make a link INSIDE a string resource (along with other text in it) ? - Android

查看:26
本文介绍了如何在字符串资源中创建链接(以及其中的其他文本)?- 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:看看下面的字符串资源:

For example: Take a look at the following string resource:

 <string name="b1b">This link will take you to google.com. More text here.</string>

现在我希望这个字符串资源在我的应用程序中看起来像这样:

Now I want this string resource to look like this in my app:

此链接会将您带到 Google.更多文字在这里.

This link will take you to Google. More text here.

我不能使用三个文本视图.这只是一个例子.所以我不能让整个 textview 成为一个链接.

I can't use three textviews. This was just an example. So I can't make the entire textview a link.

(为什么?我在我的应用程序中所做的是……我已经说了十几个按钮,每个按钮都将一个字符串资源 ID 作为意图发送到文本淋浴活动"……而且我只有一个文本视图,它显示基于不同的文本用户点击了哪个按钮.所以,我正在节省应用大小.

(Why? What I'm doing in my app is … I have say a dozen buttons, each of them sends a string resource ID as an intent to a "Text Shower Activity" … and in that I simply have a single textview which shows different texts based on which button the user clicked. So, I'm saving on app size.

此外,每个这样的文本字符串资源在不同的地方都有不同数量的链接,所以只为链接使用一个文本视图并以某种方式将其编织在两者之间是不可行的.)

Plus every such text string resource has different number of links at different places, so it's not feasible to have a single textview just for links and somehow weave it in between.)

所以,我需要将一些字符串资源制作成一个链接.我试过 <a> 没有效果.

So, I need to make a little bit of the string resource into a link. I've tried the <a> thing with no effect.

如何做到这一点?

推荐答案

示例 XML 资源:

</string name="mlink">
    to go to Google<![CDATA[ <a href="http://google.com">click here</a>]]> 
    and <![CDATA[<a href="http://yahoo.com">this</a>]]> moves you to yahoo!
</string>

Java:

yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));

请注意,您需要将 html 链接放在 CDATA 标签内,这是在 String 资源中使用链接的正确方法.

Note that you need to put your html link inside the CDATA tag, this is the proper way to use links in String resources.

这篇关于如何在字符串资源中创建链接(以及其中的其他文本)?- 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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