[html] package com.example.textviewfromhtml; import java.net.URL; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.StrictMode; import android.text.Html; import android.text.Html.ImageGetter; import android.text.method.LinkMovementMethod; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override www.2cto.com protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); struct(); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.txt); 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><p><font color=\"#aabb00\">顏色1" + "</p><p><font color=\"#00bbaa\">顏色2</p><p><font color=\"#aabb00\">顏色1" + "</p><p><font color=\"#00bbaa\">顏色2</p><p><font color=\"#aabb00\">顏色1" + "</p><p><font color=\"#00bbaa\">顏色2</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://www.bkjia.com/uploads/allimg/131211/01025S3D-0.jpg\"/></body></html>"; tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 設定可滾動 tv.setMovementMethod(LinkMovementMethod.getInstance());//設定超連結可以開啟網頁 tv.setText(Html.fromHtml(html, imgGetter, null)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } //這裡面的resource就是fromhtml函數的第一個參數裡面的含有的url ImageGetter imgGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Log.i("RG", "source---?>>>" + source); Drawable drawable = null; URL url; try { url = new URL(source); Log.i("RG", "url---?>>>" + url); drawable = Drawable.createFromStream(url.openStream(), ""); // 擷取網路圖片 } catch (Exception e) { e.printStackTrace(); return null; } drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); Log.i("RG", "url---?>>>" + url); return drawable; } }; public static void struct() { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() // or // .detectAll() // for // all // detectable // problems .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() // 探測SQLite資料庫操作 .penaltyLog() // 列印logcat .penaltyDeath().build()); } } 記得添加許可權:<uses-permission android:name="android.permission.INTERNET" />效果如下: