Android TextView 加载MarkDown 格式的数据,支持富文本格式

前言:项目开发中经常需要一些文字显示不同样式 比如:加粗 换行 添加图片 链接调整等等,这个时候大都用安卓自带的Spannable, 如果特殊样式的字体比较少Spannable或者html 还是可以的。但是如果客户端要支持复杂样式的文案 Spannable 就不是非常灵活了 ,这种情况下很多小伙伴都会想到用富文本的HTML也就是用WebView,但是WebView比较消耗性能 如果在消息表中使用不太合适,然后大家还会想到用MarkDown,但是Android 原生对MarkDown的支持不是太好,所以大家一般都引入库做。

网上封装的比较好的库有:RichText 和 Markwon

  1. RichText: https://github.com/zzhoujay/RichText

Android平台下的富文本解析器
  • 流式操作

  • 低侵入性

  • 依赖少,只依赖了disklrucachesupport v4

  • 支持Html和Markdown格式文本

  • 支持图片点击和长按事件

  • 链接点击事件和长按事件

  • 支持设置加载中和加载错误时的图片

  • 支持自定义超链接的点击回调

  • 支持修正图片宽高

  • 支持GIF图片

  • 支持Base64编码、本地图片和Assets目录图片

  • 自持自定义图片加载器、图片加载器

  • 支持内存和磁盘双缓存

  • 已经加入对自定义Html解析器的支持

效果:

使用:gradle中引用的方法:

implementation 'com.zzhoujay.richtext:richtext:3.0.7'  //RichText资源库

代码中使用:

因为是富文本,内容是会很多的,所以还得加个滚动条(我这里就不展示出来了)

 <TextView
    android:id="@+id/goods_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Java代码:

 RichText.initCacheDir(GoodDetail.this); //设置缓存目录,不设置会报错
  String goods_details_content = goodsList.getString("goods_details"); //后台获取到的数据资源
 RichText.from(goods_details_content)
         .bind(GoodDetail.this)
         .showBorder(false)
         .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
         .into(goods_details);
 @Override
    protected void onDestroy() {
        super.onDestroy();
        //结束时清空内容
        RichText.clear(GoodDetail.this);
    }

2. 使用:Markwonhttps://github.com/noties/Markwon

比较新的库,一直在更新:

Markwon is a markdown library for Android. It parses markdown following commonmark-spec with the help of amazing commonmark-java library and renders result as Android-native Spannables. No HTML is involved as an intermediate step. No WebView is required. It's extremely fast, feature-rich and extensible.

It gives ability to display markdown in all TextView widgets (TextView, Button, Switch, CheckBox, etc), Toasts and all other places that accept Spanned content. Library provides reasonable defaults to display style of a markdown content but also gives all the means to tweak the appearance if desired. All markdown features listed in commonmark-spec are supported (including support for inlined/block HTML code, markdown tables, images and syntax highlight).

Markwon comes with a sample application. It is a collection of library usages that comes with search and source code for each code sample.

Since version 4.2.0 Markwon comes with an editor to highlight markdown input as user types (for example in EditText).

使用:引入库:

  implementation "io.noties.markwon:core:4.6.2"
    implementation 'io.noties.markwon:image:4.6.2'
    implementation 'io.noties.markwon:image-glide:4.6.2'

 android使用demo代码kotlin写法:

 val content = "![RUNOOB 图标](http://static.runoob.com/images/runoob-logo.png)
\n" +
              "\n" +
              "1. 第一项:\n" +
              "    - 第一项嵌套的第一个元素\n" +
              "    - 第一项嵌套的第二个元素\n" +
              "\n" +
              "这是一个链接 [菜鸟教程](https://www.runoob.com)\n"
      val markwon = Markwon.builder(this)
          .usePlugin(GlideImagesPlugin.create(this))
          .usePlugin(GlideImagesPlugin.create(Glide.with(this)))
          .usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore {
              override fun cancel(target: Target<*>) {
                  Glide.with(this@TestActivity).clear(target);
              }
              override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> {
                  return Glide.with(this@TestActivity).load(drawable.destination);
              }
          }))
          .build()
      markwon.setMarkdown(textView, content)

上面是显示markdown富文本demo,如果想要显示html也是可以的,不过要添加对应的html依赖库。

猜你喜欢

转载自blog.csdn.net/Jason_HD/article/details/128851635
今日推荐