2011-04-22 102 views
4

我試圖在about框中添加指向Twitter個人資料的鏈接。 「常規」的鏈接,如電子郵件地址和網址由無法讓HREF在Android strings.xml中工作

android:autoLink="email|web" 

在about.xml處理,但對於Twitter的個人資料頁,我需要在我的strings.xml使用HTML代碼。我已經試過:

<string name="twitter">Follow us on &lt;a href=\"http://www.twitter.com/mytwitterprofile"&gt;Twitter: @mytwitterprofile&lt;/a&gt;</string> 

這使得在關於對話框HTML標記。

我也試過:

<string name="twitter">Follow us on <a href="http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></string> 

其中顯示文本「在Twitter上關注我們:@mytwitterprofile」,但它不是一個超鏈接。

我該如何做這個看似簡單的任務!?

乾杯, 巴里

+0

你如何將字符串添加到您的框? – rajath 2011-04-22 12:52:01

+0

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

+0

看看你是否覺得這篇文章有用 - http://stackoverflow.com/questions/1997328/android-clickable-hyperlinks-in-alertdialog – rajath 2011-04-22 12:56:54

回答

5

簡單答案是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方法某人否則張貼在答案)。

+0

謝謝你。這不是完美的,但它會做。順便說一句,我不需要Html.fromHtml(我的about.xml有android:autoLink =「email | web」雖然) – barry 2011-04-22 13:13:05

2

我也不太清楚如何使用「串」鏈接,但你可以使用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()); 
+0

感謝您的回覆,但這只是顯示「谷歌鏈接!」,而不是超鏈接。 – barry 2011-04-22 12:55:31

+0

text.setMovementMethod(LinkMovementMethod.getInstance());應該把它變成一個可點擊的鏈接?! – 2011-04-22 12:56:13

+0

不幸的是,只是普通的文字。 – barry 2011-04-22 13:01:17

7

問題是您的「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()); 
+2

我想關閉CDATA部分它應該是]]>而不是]]。示例文本<![CDATA [link1]]> 2013-01-16 19:40:23

+0

是的,謝謝你的收穫。編輯。 – dule 2013-01-17 21:53:02