[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.zzzyk.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=/2013/0128/20130128020940641.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数据库操作