本文主要介绍 Mezzanine 和 jsoup。 其中 Mezzanine 用于将 html/js 页面转成字符串; jsoup 用于解析 html 代码。
Mezzanine: https://github.com/anthonycr/Mezzanine
jsoup: https://github.com/jhy/jsoup
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
def hash = new HashMap()
hash.put("mezzanine.projectPath", project.rootDir.getAbsolutePath())
arguments = hash
}
}
}
}
dependencies {
// file reading
implementation "com.anthonycr.mezzanine:mezzanine:1.1.1"
annotationProcessor "com.anthonycr.mezzanine:mezzanine-compiler:1.1.1"
// html parsing for reading mode
implementation 'org.jsoup:jsoup:1.15.2'
}
在 app/src/main/html/ 文件夹中新建 index.html 文件。
标题
小标题
新建 IndexPage.java 文件。
@FileStream("app/src/main/html/index.html")
public interface IndexPage {
String provideHtml();
}
调用代码
MezzanineGenerator.IndexPage licensePage = new MezzanineGenerator.IndexPage();
Document document = Jsoup.parse(licensePage.provideHtml());
// 修改网页标题
document.title("这是网页的标题");
// 根据 id 修改节点内容
document.getElementById("title1").text("标题1");
// 输出 html 字符串
String html = document.outerHtml();
TextView tvShow = findViewById(R.id.tvShow);
// 可选, 如果需要 TextView 中的超链接可以点击, 需要设置下面两个属性
tvShow.setClickable(true);
tvShow.setMovementMethod(LinkMovementMethod.getInstance());
tvShow.setText(Html.fromHtml(html));
