Android TextView 显示 HTML

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31876841/article/details/88354289

直接上代码

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        mContextTextView.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY));
      } else {
        mContextTextView.setText(Html.fromHtml(content));
      }

那么,HTML 中的图片怎么办呢? 文档 中就有提到

Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don’t want this)

所以,我们只要利用 fromHtml (String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) 中的 ImageGetter 就可以显示图片了。

  private class CoinImageGetter implements Html.ImageGetter {
    @Override
    public Drawable getDrawable(String source) {

      Drawable d = ContextCompat.getDrawable(mContext, R.drawable.account_icon_ecoin);
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
      return d;
    }
  }

TagHandler 则是可以拦截其他元素为其赋值。

猜你喜欢

转载自blog.csdn.net/qq_31876841/article/details/88354289