参考文档
网上现成的代码有个RichText;
第二种是MarkDownView 这个是基于WebView实现的;不符合要求【需要用在列表中的】。
Markdown基本语法 - 简书 (jianshu.com)
还有文档Android TextView富文本深入探索 - 简书 (jianshu.com)
基于android Jetpack Compose 实现的
Implement Markdown Text in Android using Jetpack Compose - GeeksforGeeks
GitHub - noties/Markwon: Android markdown library (no WebView)
解决方案:
1、先将需求分解为两部分 markDown->Html->TextView照这个思路去查阅就能发现
发现几个不错的库MD-Tool+Html-Text
这两个是个分开的两个库是讲需求分解之后找出来做组装的可以作为参考
原理分析:
站在巨人的肩膀上来学习!查看这几个库的源码发现 核心思想就是
将文本 进行分块切割,使用正则匹配 替换字符等;
Html.formHtml()
String htmlText1 = "<b>Hello World</b>";
text1.setText(Html.fromHtml(htmlText1));
String htmlText2 = "<font color='#ff0000'>Hello World</font>";
text2.setText(Html.fromHtml(htmlText2));
String htmlText3 = "<i><a href='https://gavinli369.github.io/'>我的博客</a></i>";
text3.setMovementMethod(LinkMovementMethod.getInstance());
text3.setText(Html.fromHtml(htmlText3));
再看看textView提供的style不难就会发现一些html标签其实也可以归整到这些style中
HTML
在android有个Html.java
/**
* This class processes HTML strings into displayable styled text.
* Not all HTML tags are supported.
*/
public class Html {
/**
* Retrieves images for HTML <img> tags.
*/
public static interface ImageGetter {
/**
* This method is called when the HTML parser encounters an
* <img> tag. The <code>source</code> argument is the
* string from the "src" attribute; the return value should be
* a Drawable representation of the image or <code>null</code>
* for a generic replacement image. Make sure you call
* setBounds() on your Drawable if it doesn't already have
* its bounds set.
*/
public Drawable getDrawable(String source);
}
/**
* Is notified when HTML tags are encountered that the parser does
* not know how to interpret.
*/
public static interface TagHandler {
/**
* This method will be called whenn the HTML parser encounters
* a tag that it does not know how to interpret.
*/
public void handleTag(boolean opening, String tag,
Editable output, XMLReader xmlReader);
}
/**
* Option for {@link #toHtml(Spanned, int)}: Wrap consecutive lines of text delimited by '\n'
* inside <p> elements. {@link BulletSpan}s are ignored.
*/
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0x00000000;
/**
* Option for {@link #toHtml(Spanned, int)}: Wrap each line of text delimited by '\n' inside a
* <p> or a <li> element. This allows {@link ParagraphStyle}s attached to be
* encoded as CSS styles within the corresponding <p> or <li> element.
*/
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 0x00000001;
/**
* Flag indicating that texts inside <p> elements will be separated from other texts with
* one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 0x00000001;
/**
* Flag indicating that texts inside <h1>~<h6> elements will be separated from
* other texts with one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 0x00000002;
/**
* Flag indicating that texts inside <li> elements will be separated from other texts
* with one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 0x00000004;
/**
* Flag indicating that texts inside <ul> elements will be separated from other texts
* with one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 0x00000008;
/**
* Flag indicating that texts inside <div> elements will be separated from other texts
* with one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 0x00000010;
/**
* Flag indicating that texts inside <blockquote> elements will be separated from other
* texts with one newline character by default.
*/
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 0x00000020;
/**
* Flag indicating that CSS color values should be used instead of those defined in
* {@link Color}.
*/
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 0x00000100;
/**
* Flags for {@link #fromHtml(String, int, ImageGetter, TagHandler)}: Separate block-level
* elements with blank lines (two newline characters) in between. This is the legacy behavior
* prior to N.
*/
public static final int FROM_HTML_MODE_LEGACY = 0x00000000;
/**
* Flags for {@link #fromHtml(String, int, ImageGetter, TagHandler)}: Separate block-level
* elements with line breaks (single newline character) in between. This inverts the
* {@link Spanned} to HTML string conversion done with the option
* {@link #TO_HTML_PARAGRAPH_LINES_INDIVIDUAL}.
*/
public static final int FROM_HTML_MODE_COMPACT =
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH
| FROM_HTML_SEPARATOR_LINE_BREAK_HEADING
| FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM
| FROM_HTML_SEPARATOR_LINE_BREAK_LIST
| FROM_HTML_SEPARATOR_LINE_BREAK_DIV
| FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE;
/**
* The bit which indicates if lines delimited by '\n' will be grouped into <p> elements.
*/
private static final int TO_HTML_PARAGRAPH_FLAG = 0x00000001;
这是其中一部分代码 可以看到html标签被定义,而且Html类内部还是在使用Spannable处理,我们会在后文看到它的实现方式。