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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值