Android markdown富文本渲染解析实现

it2026-09-25  13

MarkDown

Android平台的原生Markdown解析器,已整合进 RichText

由markdown文本直接转换为Spanned,快捷高效不依赖特定控件,低侵入性遵循 Github Flavored Markdown 标准

目录

MarkDown效果展示使用自定义样式在RichText中使用Use in Gradle已知问题后续计划License

效果展示

使用

Markdown.fromMarkdown(text,imageGetter,textView);

注意: 此方法需要在textView的Measure完成后调用,因为需要获取textView的宽高

例子:

textView.post(new Runnable() { @Override public void run() { Spanned spanned = MarkDown.fromMarkdown(stream, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); drawable.setBounds(0, 0, 400, 400); return drawable; } }, textView); textView.setText(spanned); }

自定义样式

在可以你的主题里加入 markdownStyle 来自定义样式,如果没有指定样式会使用自带的亮色主题。

例子:

<style name="你的主题"> <!--<item name="markdownStyle">@style/Markdown</item>--> <!-- 暗色主题 --> <item name="markdownStyle">@style/Markdown.Light</item> <!-- 亮色主题 --> </style>

在RichText中使用

RichText 包含了一些对图片和其它东西的处理,使用更简单

RichText.fromMarkdown(markdown).into(textView);

Use in Gradle

compile 'com.zzhoujay.markdown:markdown:1.0.5'

已知问题

暂不支持使用反斜杠 \ 转义不支持表格TextView设置了setTextIsSelectable(true)后和长按事件冲突暂时不支持内嵌html

后续计划

修复一些已知问题加入多选框支持增加对简单html标签的支持

License

The MIT License (MIT) Copyright (c) 2015 zzhoujay Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
最新回复(0)