TextView或Toast显示格式化的html字符串

s

s

TextView或Toast显示格式化的html字符串

((Button) findViewById(R.id.button_test_toast1)).setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
		//ToastUtil.ToastShort(MainActivity.this, "第一行!\n第二行");
		//ToastUtil.ToastShort(MainActivity.this, Html.fromHtml("<h1 align='center'>标题!</h1><p><font color='red'>红色</font><u>下划线</u><i>斜体</i></p>"));
	
		Spanned sp = Html.fromHtml("<h1 align='center'>标题!</h1><p><font color='red'>红色</font><u>下划线</u><i>斜体</i><img src='https://www.google.com.hk/intl/zh-CN/images/logo_cn.png' /></p>", new Html.ImageGetter() {
			@Override
			public Drawable getDrawable(String source) {
				InputStream is = null;
				try {
					is = (InputStream) new URL(source).getContent();
					Drawable d = Drawable.createFromStream(is, "src");
					d.setBounds(0, 0, d.getIntrinsicWidth(),
							d.getIntrinsicHeight());
					is.close();
					return d;
				} catch (Exception e) {
					return null;
				}
			}
		}, null);
		ToastUtil.ToastShort(MainActivity.this, sp);
	}
});

//上面图片加载是同步地从网络上下载,而且没有缓存。实际用的话需要优化。这里只是功能Demo

//而且上面的align='center'属性并没有生效。我估计是不支持吧。


    public static void ToastShort(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }


s



s


你可能感兴趣的:(html,exception,优化,String,null,button)