【Andoid杂谈】Android TextView中显示超文本(HTML)内容
2015-10-20 19:43
633 查看
主要在于html.fromHtml();方法的应用,
以下是代码:
<span style="font-size:18px;"><strong>package yyc.com.htmltextview; import android.graphics.drawable.Drawable; import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Html; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import java.net.URL; import yyc.com.htmltextview.tools.HtmlTextView; public class MainActivity extends AppCompatActivity { TextView htmlTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); struct(); init(); } 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()); } private void init(){ htmlTextView=(TextView)findViewById(R.id.html); String s="<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>"; //以下放置内容HTML超文本内容 htmlTextView.setText(Html.fromHtml(s,imgGetter,null)); } //这里面的resource就是fromhtml函数的第一个参数里面的含有的url Html.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; } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }</strong></span>
以下是代码:
相关文章推荐
- 跟Android源码:获取文件的缓存大小
- android VideoView实现视频的播放
- [转]Android网格视图(GridView)
- Android Studio找不到View类
- android 国际区号注册手机号编码 以及常用城市列表
- 爬网易首页新闻头条
- Android开发手记(12) Menu的使用
- android倒计时
- 编译错误:com.android.dex.DexException:
- Android的屏幕多样性支持
- android support-v4、v7、v13
- Android学习路线
- 产生二维码的Android例子
- android 限制输入字符数!看清楚:是限制字符数,不是字数!中英混排?就算emoji也不怕啦!
- ContentResolver读取系统联系人数据
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(四)
- Android文字局部变色 TextView文字部分变色代码
- AndroidStudio 怎样导入jar包
- 如何更新android sdk
- [Android] 环境配置之Android Studio开发NDK