首页 > 代码库 > TEXTview 加载html

TEXTview 加载html

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

TEXTview 加载html