學Android的時候忽然想到一個問題:怎麼用TextView控件顯示帶有格式的文字,能否使用Html佈局?查了下Android 幫助文檔,其提供了android.text.Html類和Html.ImageGetter、Html.TagHandler接口。 html
其實本不打算寫這篇博文的,但看到網絡上關於此的文章,基本是:你抄我,我抄你,你們抄來抄去,有用的也就那麼一兩篇文章,並且說得不明不白,網絡就是如此,盜版也成爲了一種文化,這就是所謂的拿來主義吧。固然不否定大牛的辛勤勞做,寫出的高質量文章;其次是學以至用,我的習慣--總結一下。 java
先看截圖: android

咱們日常使用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);
- }
而String類是CharSequence的子類,在CharSequence子類中有一個接口Spanned,即相似html的帶標記的文本,咱們能夠用它來在TextView中顯示html。但在上面Android源碼註釋中有說起TextView does not accept HTML-like formatting。
android.text.Html類共提供了三個方法,能夠到Android幫助文檔查看。 ide
- 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";