android TextView加载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
- 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://avatar.csdn.net/0/3/8/2_zhang957411207.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" />
效果如下:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。