0%

Android富文本功能记录

Android富文本功能记录

从assets中加载本地WebView模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void getAssetsHtml(Context context, WebView webView,String contentText){

try {
AssetManager assetManager = context.getAssets();
//InputStream stream = assetManager.open("page.html");
InputStream stream = assetManager.open("newsyun.html");
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append("\n");
}
//String str_div_container = "<div class=\"newsyun_container\"></div>";
String str_div_container = "<body>BODY_CONTAINER</body>";
if (total.toString().contains(str_div_container)){
String[] split = total.toString().split(str_div_container, -1);
String newString = split[0] + contentText + split[1];
webView.loadDataWithBaseURL(null, newString, "text/html", "UTF-8", null);
}
//webView.loadDataWithBaseURL(null, total.toString(), "text/html", "UTF-8", null);
} catch (Exception xxx) {
Log.e(TAG, "Load assets/page.html", xxx);
}
}

newsyun.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, maximum-scale=1, minimum-scale=1, user-scale=1">
<title>华舆</title>
<link rel="stylesheet" type="text/css" href="file:///android_asset/newsyun.css" />
<script type="text/javascript" src="file:///android_asset/js/jquery.min.js"></script>
<script type="text/javascript" src="file:///android_asset/js/jquery.lazyload.min.js"></script>
<script type="text/javascript" src="file:///android_asset/js/newsyun.js"></script>
</head>
<body>BODY_CONTAINER</body>
</html>

newsyun.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$(document).ready(function () {
var imgs = document.getElementsByTagName('img');
for (var i = 0; i < imgs.length; i++) {
imgs[i].index = i;
imgs[i].onclick = function () {
var j = this.index;
window.imagelistener.openImage(this.src, this.index);
}
}

$("audio").width("100%");
$("video").width("100%");

$("img").width("100%");
$("img").height("auto");

$('video').addClass("newsyun_video_or_audio");
$('audio').addClass("newsyun_video_or_audio");

$("img").each(function () {
$(this).attr("data-original", $(this).attr("src"));
$(this).removeAttr("src");
});

$("img").lazyload({
effect: "fadeIn",
placeholder: "file:///android_asset/pic_16_9.png"
});

$('.newsyun_video_or_audio').each(function (i) {
/*$(this).get().*/
$(this).on('play', function () {
var thi = $(this).get();
var other = $(this).siblings('.newsyun_video_or_audio').get();
for (var i = 0; i < other.length; i++) {
other[i].pause();
}
});
});

});

String.prototype.startWith = function (compareStr) {
return this.indexOf(compareStr) == 0;
}