首页 > 代码库 > TEXTview 加载html
TEXTview 加载html
- 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.mamicode.com/"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.mamicode.com/"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());
- }
- }
TEXTview 加载html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。