android之TextView使用HTML處理字體樣式、顯示圖片等

學Android的時候突然想到一個問題:怎麼用TextView控件顯示帶有格式的文字,可否使用Html佈局?查了下Android 幫助文檔,其提供了android.text.Html類和Html.ImageGetter、Html.TagHandler接口

        其實本不打算寫這篇博文的,但看到網絡上關於此的文章,基本是:你抄我,我抄你,大家抄來抄去,有用的也就那麼一兩篇文章,而且說得不明不白,網絡就是如此,盜版也成爲了一種文化,這就是所謂的拿來主義吧。當然不否認大牛的辛勤勞作,寫出的高質量文章;其次是學以致用,個人習慣--總結一下。

先看截圖:

               

        我們平常使用TextView的setText()方法傳遞String參數的時候,其實是調用的public final void setText (CharSequence text)方法:

[java] view plaincopy
  1. /** 
  2.     * Sets the string value of the TextView. TextView <em>does not</em> accept 
  3.     * HTML-like formatting, which you can do with text strings in XML resource files. 
  4.     * To style your strings, attach android.text.style.* objects to a 
  5.     * {@link android.text.SpannableString SpannableString}, or see the 
  6.     * <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources"> 
  7.     * Available Resource Types</a> documentation for an example of setting  
  8.     * formatted text in the XML resource file. 
  9.     * 
  10.     * @attr ref android.R.styleable#TextView_text 
  11.     */  
  12.    @android.view.RemotableViewMethod  
  13.    public final void setText(CharSequence text) {  
  14.        setText(text, mBufferType);  
  15.    }  
        而String類是CharSequence的子類,在CharSequence子類中有一個接口Spanned,即類似html的帶標記的文本,我們可以用它來在TextView中顯示html。在上面Android源碼註釋中有提及TextView does not accept HTML-like formatting。

       android.text.Html類共提供了三個方法,可以到Android幫助文檔查看。

[java] view plaincopy
  1. public static Spanned fromHtml (String source)  
  2.   
  3. public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)  
  4.   
  5. public static String toHtml (Spanned text)  


       通過使用第一個方法,可以將Html顯示在TextView中:

[java] view plaincopy
  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.main);  
  4.   
  5.         TextView tv=(TextView)findViewById(R.id.textView1);  
  6.         String html="<html><head><title>TextView使用HTML</title></head><body><p><strong>強調</strong></p><p><em>斜體</em></p>"  
  7.                 +"<p><a href=\"http://www.dreamdu.com/xhtml/\">超鏈接HTML入門</a>學習HTML!</p><p><font color=\"#aabb00\">顏色1"  
  8.                 +"</p><p><font color=\"#00bbaa\">顏色2</p><h1>標題1</h1><h3>標題2</h3><h6>標題3</h6><p>大於>小於<</p><p>" +  
  9.                 "下面是網絡圖片</p><img src=\"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";  
  10.           
  11.         tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滾動  
  12.         tv.setText(Html.fromHtml(html));      
  13.     }  
效果:

              
        可以看出,字體效果是顯示出來了,但是圖片卻沒有顯示。要實現圖片的顯示需要使用Html.fromHtml的另外一個重構方法:public static Spanned fromHtml (String source, Html.ImageGetterimageGetter, Html.TagHandler tagHandler)其中Html.ImageGetter是一個接口,我們要實現此接口,在它的getDrawable(String source)方法中返回圖片的Drawable對象纔可以。
修改後的代碼:

[java] view plaincopy
  1. ImageGetter imgGetter = new Html.ImageGetter() {  
  2.         public Drawable getDrawable(String source) {  
  3.               Drawable drawable = null;  
  4.               URL url;    
  5.               try {     
  6.                   url = new URL(source);    
  7.                   drawable = Drawable.createFromStream(url.openStream(), "");  //獲取網路圖片  
  8.               } catch (Exception e) {    
  9.                   return null;    
  10.               }    
  11.               drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable  
  12.                             .getIntrinsicHeight());  
  13.               return drawable;   
  14.         }  
  15. };  

這裏主要是實現了Html.ImageGetter接口,通過圖片的URL地址獲取相應的Drawable實例。
不要忘了在Mainifest文件中加入網絡訪問的權限:

[java] view plaincopy
  1. <uses-permission android:name="android.permission.INTERNET" />  

 友情提示:通過網絡獲取圖片是一個耗時的操作,最好不要放在主線程中,否則容易引起阻塞。
上面介紹的是顯示網絡上的圖片,但如何顯示本地的圖片呢:


[java] view plaincopy
  1.    ImageGetter imgGetter = new Html.ImageGetter() {  
  2.         public Drawable getDrawable(String source) {  
  3.               Drawable drawable = null;  
  4.                  
  5.               drawable = Drawable.createFromPath(source); //顯示本地圖片  
  6.               drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable  
  7.                             .getIntrinsicHeight());  
  8.               return drawable;   
  9.         }  
  10. };  
只需將source改爲本地圖片的路徑便可,在這裏我使用的是:

[java] view plaincopy
  1. String source;  
  2. source=getFilesDir()+"/ic_launcher.png"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章