學Android的時候突然想到一個問題:怎麼用TextView控件顯示帶有格式的文字,可否使用Html佈局?查了下Android 幫助文檔,其提供了android.text.Html類和Html.ImageGetter、Html.TagHandler接口。
其實本不打算寫這篇博文的,但看到網絡上關於此的文章,基本是:你抄我,我抄你,大家抄來抄去,有用的也就那麼一兩篇文章,而且說得不明不白,網絡就是如此,盜版也成爲了一種文化,這就是所謂的拿來主義吧。當然不否認大牛的辛勤勞作,寫出的高質量文章;其次是學以致用,個人習慣--總結一下。
先看截圖:
我們平常使用TextView的setText()方法傳遞String參數的時候,其實是調用的public final void setText (CharSequence text)方法:
- /**
- * 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;
- }
- };
- String source;
- source=getFilesDir()+"/ic_launcher.png";