Android TextView使用HTML處理字型樣式、顯示圖片等
學Android的時候突然想到一個問題:怎麼用TextView控制元件顯示帶有格式的文字,可否使用Html佈局?查了下Android 幫助文件,其提供了android.text.Html類和Html.ImageGetter、Html.TagHandler介面。
其實本不打算寫這篇博文的,但看到網路上關於此的文章,基本是:你抄我,我抄你,大家抄來抄去,有用的也就那麼一兩篇文章,而且說得不明不白,網路就是如此,盜版也成為了一種文化,這就是所謂的拿來主義吧。當然不否認大牛的辛勤勞作,寫出的高質量文章;其次是學以致用,個人習慣--總結一下。
先看截圖:
我們平常使用TextView的setText()方法傳遞String引數的時候,其實是呼叫的public final void setText (CharSequence text)方法:
而String類是CharSequence的子類,在CharSequence子類中有一個介面Spanned,即類似html的帶標記的文字,我們可以用它來在TextView中顯示html。但在上面Android原始碼註釋中有提及TextView does not accept HTML-like formatting。/** * Sets the string value of the TextView. TextView <em>does not</em> accept * HTML-like formatting, which you can do with text strings in XML resource files. * To style your strings, attach android.text.style.* objects to a * {@link android.text.SpannableString SpannableString}, or see the * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources"> * Available Resource Types</a> documentation for an example of setting * formatted text in the XML resource file. * * @attr ref android.R.styleable#TextView_text */ @android.view.RemotableViewMethod public final void setText(CharSequence text) { setText(text, mBufferType); }
android.text.Html類共提供了三個方法,可以到Android幫助文件檢視。
public static Spanned fromHtml (String source) public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) public static String toHtml (Spanned text)
通過使用第一個方法,可以將Html顯示在TextView中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.textView1);
String html="<html><head><title>TextView使用HTML</title></head><body><p><strong>強調</strong></p><p><em>斜體</em></p>"
+"<p><a href=\"http://www.dreamdu.com/xhtml/\">超連結HTML入門</a>學習HTML!</p><p><font color=\"#aabb00\">顏色1"
+"</p><p><font color=\"#00bbaa\">顏色2</p><h1>標題1</h1><h3>標題2</h3><h6>標題3</h6><p>大於>小於<</p><p>" +
"下面是網路圖片</p><img src=\"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";
tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滾動
tv.setText(Html.fromHtml(html));
}
效果:
可以看出,字型效果是顯示出來了,但是圖片卻沒有顯示。要實現圖片的顯示需要使用Html.fromHtml的另外一個重構方法:public static Spanned fromHtml (String source, Html.ImageGetterimageGetter, Html.TagHandler tagHandler)其中Html.ImageGetter是一個介面,我們要實現此介面,在它的getDrawable(String source)方法中返回圖片的Drawable物件才可以。
修改後的程式碼:
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;
}
};
這裡主要是實現了Html.ImageGetter介面,通過圖片的URL地址獲取相應的Drawable例項。
不要忘了在Mainifest檔案中加入網路訪問的許可權:
<uses-permission android:name="android.permission.INTERNET" />
友情提示:通過網路獲取圖片是一個耗時的操作,最好不要放在主執行緒中,否則容易引起阻塞。
上面介紹的是顯示網路上的圖片,但如何顯示本地的圖片呢:
ImageGetter imgGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
drawable = Drawable.createFromPath(source); //顯示本地圖片
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
只需將source改為本地圖片的路徑便可,在這裡我使用的是: String source;
source=getFilesDir()+"/ic_launcher.png";
THE END