首页 > 代码库 > android TextView加载html内容并加载图片

android TextView加载html内容并加载图片

  1. package com.example.textviewfromhtml;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.os.Bundle;  
  8. import android.os.StrictMode;  
  9. import android.text.Html;  
  10. import android.text.Html.ImageGetter;  
  11. import android.text.method.LinkMovementMethod;  
  12. import android.text.method.ScrollingMovementMethod;  
  13. import android.util.Log;  
  14. import android.view.Menu;  
  15. import android.widget.TextView;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         struct();  
  23.         setContentView(R.layout.activity_main);  
  24.         TextView tv = (TextView) findViewById(R.id.txt);  
  25.         String html = "<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"  
  26.                 + "<p><href=http://www.mamicode.com/"http://www.dreamdu.com/xhtml/\">超链接HTML入门</a>学习HTML!</p><p><font color=\"#aabb00\">颜色1"  
  27.                 + "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"  
  28.                 + "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"  
  29.                 + "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"  
  30.                 + "</p><p><font color=\"#00bbaa\">颜色2</p><p><font color=\"#aabb00\">颜色1"  
  31.                 + "</p><p><font color=\"#00bbaa\">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>"  
  32.                 + "下面是网络图片</p><img src=http://www.mamicode.com/"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";  
  33.   
  34.         tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动  
  35.         tv.setMovementMethod(LinkMovementMethod.getInstance());//设置超链接可以打开网页  
  36.         tv.setText(Html.fromHtml(html, imgGetter, null));  
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onCreateOptionsMenu(Menu menu) {  
  41.         // Inflate the menu; this adds items to the action bar if it is present.  
  42.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  43.         return true;  
  44.     }  
  45.         //这里面的resource就是fromhtml函数的第一个参数里面的含有的url  
  46.     ImageGetter imgGetter = new Html.ImageGetter() {  
  47.         public Drawable getDrawable(String source) {  
  48.             Log.i("RG", "source---?>>>" + source);  
  49.             Drawable drawable = null;  
  50.             URL url;  
  51.             try {  
  52.                 url = new URL(source);  
  53.                 Log.i("RG", "url---?>>>" + url);  
  54.                 drawable = Drawable.createFromStream(url.openStream(), ""); // 获取网路图片  
  55.             } catch (Exception e) {  
  56.                 e.printStackTrace();  
  57.                 return null;  
  58.             }  
  59.             drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
  60.                     drawable.getIntrinsicHeight());  
  61.             Log.i("RG", "url---?>>>" + url);  
  62.             return drawable;  
  63.         }  
  64.     };  
  65.   
  66.     public static void struct() {  
  67.         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
  68.                 .detectDiskReads().detectDiskWrites().detectNetwork() // or  
  69.                                                                         // .detectAll()  
  70.                                                                         // for  
  71.                                                                         // all  
  72.                                                                         // detectable  
  73.                                                                         // problems  
  74.                 .penaltyLog().build());  
  75.         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
  76.                 .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作  
  77.                 .penaltyLog() // 打印logcat  
  78.                 .penaltyDeath().build());  
  79.     }  
  80. }  

记得添加权限:<uses-permission android:name="android.permission.INTERNET" />

效果如下:

技术分享

android TextView加载html内容并加载图片