Can';t让HREF在Android strings.xml中工作
我正试图在“关于”框中添加Twitter个人资料的链接电子邮件地址和网址等常规链接由Can';t让HREF在Android strings.xml中工作,android,href,Android,Href,我正试图在“关于”框中添加Twitter个人资料的链接电子邮件地址和网址等常规链接由 android:autoLink="email|web" 在about.xml中,但对于Twitter配置文件页面,我需要在我的strings.xml中使用html代码。我试过: <string name="twitter">Follow us on <a href=\"http://www.twitter.com/mytwitterprofile">Twitter:
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>
关注我们的a href=\”http://www.twitter.com/mytwitterprofile“推特:@mytwitterprofile/a
它在“关于”框上呈现html标记
我也试过:
<string name="twitter">Follow us on <a href="http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></string>
继续关注我们
显示文本“在Twitter上关注我们:@mytwitterprofile”,但它不是超链接
我该如何完成这个看似简单的任务
干杯,
Barry我不太确定如何使用“字符串”链接,但您可以使用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());
TextView text=(TextView)findViewById(R.id.text);
text.setText(Html.fromHtml(“”);
setMovementMethod(LinkMovementMethod.getInstance());
简单的答案是文本视图不支持
标记。另外,它只支持基本格式,如
、
和
。但是,如果您提供android:autoLink=“web”
,则以下字符串:
<string name="twitter">Follow us at twitter.com/mytwitterprofile</string>
在twitter.com/mytwitterprofile上关注我们
将twitter.com/mytwitterprofile
转换为一个正确的链接(当通过XML设置时,如android:text=“@string/twitter”
;如果你想从代码设置,你需要别人在回答中发布的Html.fromHtml
方法)。问题是你的“a href”链接标记位于strings.xml中,在解析strings.xml时被解析为标记,这是您不希望看到的。这意味着您需要让它使用XML的CDATA忽略标记:
示例文本
然后您可以继续使用Html.fromHtml()
,并使用LinkMovementMethod
使其可单击:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
如何将字符串添加到框中?看看这篇文章是否有用-TextView TextView=(TextView)findViewById(R.id.about_content);textView.setTextColor(Color.WHITE);textView.setText(getString(R.string.about_text)、getString(R.string.twitter)、getString(R.string.email_address)、getString(R.string.website));感谢您的回复,但这只是显示“Google链接!”,而不是超链接;应该把它变成一个可点击的链接?!不幸的是,不是,只是普通文本。谢谢你。这并不完美,但可以。顺便说一句,我也不需要Html.fromHtml(虽然我的about.xml有android:autoLink=“email | web”),但我认为关闭CDATA部分应该是]]>而不是]]。示例文本