无法让HREF在Android strings.xml中工作
我试图在about框中添加指向Twitter个人资料的链接。 “常规”的链接,如电子邮件地址和网址由无法让HREF在Android strings.xml中工作
android:autoLink="email|web"
在about.xml处理,但对于Twitter的个人资料页,我需要在我的strings.xml使用HTML代码。我已经试过:
<string name="twitter">Follow us on <a href=\"http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></string>
这使得在关于对话框HTML标记。
我也试过:
<string name="twitter">Follow us on <a href="http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></string>
其中显示文本“在Twitter上关注我们:@mytwitterprofile”,但它不是一个超链接。
我该如何做这个看似简单的任务!?
干杯, 巴里
简单答案是TextView
不支持<a>
标签。 AFAIK,它只支持基本格式,如<b>
,<i>
和<u>
。但是,如果您对供应android:autoLink="web"
,以下字符串:
<string name="twitter">Follow us at twitter.com/mytwitterprofile</string>
会变成twitter.com/mytwitterprofile
成适当的链接(通过XML设置时像android:text="@string/twitter"
;如果你想从代码中设置它,您将需要Html.fromHtml
方法某人否则张贴在答案)。
谢谢你。这不是完美的,但它会做。顺便说一句,我不需要Html.fromHtml(我的about.xml有android:autoLink =“email | web”虽然) – barry 2011-04-22 13:13:05
我也不太清楚如何使用“串”链接,但你可以使用fromHtml设置的EditText TextView的或文字...
TextView text = (TextView) findViewById(R.id.text);
text.setText(Html.fromHtml("<a href=\"http://www.google.com\">Google Link!</a>"));
text.setMovementMethod(LinkMovementMethod.getInstance());
问题是您的“a href”链接标记位于strings.xml中,并在strings.xml被解析时被解析为标记,而您并不需要这些标记。这意味着你需要把它使用XML的CDATA忽略标签:
<string name="sampleText">Sample text <![CDATA[<a href="www.google.com">link1</a>]]></string>
然后你就可以用Html.fromHtml()
继续,并使其可点击与LinkMovementMethod
:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
我想关闭CDATA部分它应该是]]>而不是]]。
是的,谢谢你的收获。编辑。 – dule 2013-01-17 21:53:02
你如何将字符串添加到您的框? – rajath 2011-04-22 12:52:01
TextView textView =(TextView)findViewById(R.id.about_content); \t \t textView.setTextColor(Color.WHITE); (getString(R.string.about_text,getString(R.string.twitter),getString(R.string.email_address),getString(R.string.website)));}}; – barry 2011-04-22 16:01:48
看看你是否觉得这篇文章有用 - http://stackoverflow.com/questions/1997328/android-clickable-hyperlinks-in-alertdialog – rajath 2011-04-22 12:56:54