標籤:超連結 android from
最近需要在TextView中顯示一些超連結等資訊,如URL(點擊後跳轉),顯示網狀圖片等。
整理如下:
1.顯示URl,在需要顯示URL超連結的TextView中設定
textview.setText(Html.fromHtml(urlString))
(urlString 用html文法來標識)
versionText.setMovementMethod(LinkMovementMethod.getInstance());
上面一句必須設定,否則將無法跳轉。
2.顯示網狀圖片
textview.setText(Html.fromHtml(urlString, imgGetter, null))
調用的是Html的
public static Spanned fromHtml(String source, ImageGetter imageGetter,
TagHandler tagHandler)
需要傳入一個ImageGetter,類似的如下,返回一個Drawable:
Html.ImageGetter imgGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
URL url;
try {
url = new URL(source);
drawable = Drawable.createFromStream(url.openStream(), ""); //擷取網路圖片
} catch (Exception e) {
return null;
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
本文出自 “fangler” 部落格,謝絕轉載!