我从服务器收到xhtml代码,我希望在Android应用中显示电子书阅读器。为此,选择了 Monocle 库。


接下来,我使用以下代码准备webview:


 contentView.addJavascriptInterface(new LectureJSInterface(),“Android”);
 contentView.getSettings()。 setAllowFileAccess(true);
 contentView.getSettings()。setJavaScriptEnabled(true);
 contentView.setBackgroundColor(0xFFECECEC);
 contentView.setWebChromeClient(new WebChromeClient());
 contentView .setWebViewClient(new WebViewClient(){

 @Override
 public void onPageFinished(WebView view,String url){
 super.onPageFinished(view,url);
 Global .dismissProgressDialog();
}
});



 之后我从服务器下载xhtml代码并添加使用Monocle的JS代码:


 private String prepareCode(String code){
 if((code == null)|| code.equals(“”))return“”;

 String newCode = code.substring(code.indexOf(“< html”),code.indexOf(“< head>”)+ 6);
 newCode = newCode.concat(
“< script src = \”file:///android_asset/monocore.js \“>< / script> \ n”+
“< link rel = \“stylesheet \”type = \“text / css \”href = \“file:///android_asset/monocore.css \”/> \ n“+
”< style> \ n“ +
“#reader {\ n”+
“width:100%; \ n”+
“height:100%; \ n”+
“border:0px solid #000; \ n“+
”} \ n“+
”< / style> \ n“+
”< script> \ n“+
”\ n“+
”var isNightMode = false; \ n“+
”var isFirstLoading = false; \ n“+
”var startPageNumber = 1; \ n“+
” \ n“+
”函数setSettingsForFirstLoading(fontSize,pageNumber,nightMode){\ n“+
”Android.printLogInfo('setSettingsForFirstLoading()'); \ n“+
”isFirstLoading = true; \ n“+
”isNightMode = nightMod e; \ n“+
 “startPageNumber = pageNumber; \ n”+
 “window.changeFontSize(fontSize); \ n”+
 “} \ n”+
 “\ n”+
 “function changeFontSize(fontSize){\ n”+
 “Android.printLogInfo('changeFontSize()'); \ n”+
 “window.reader.formatting.setFontScale(fontSize); \ n”+
 “} \ n”+
 “\ n”+
 “function nightModeOn(){\ n”+
 “Android.printLogInfo('nightModeOn()'); \ n”+
 “isNightMode = true; \ n”+
 “var i = 0; \ n”+
 “var frame; \ n”+
 “while(frame = window.reader.dom.find('component',i ++)){\ n”+
 “frame.contentDocument.body.style.backgroundColor ='#1F1F1F'; \ n”+
 “frame.contentDocument.body.style.color ='#ECECEC'; \ n”+
 “} \ n”+
 “} \ n”+
 “\ n”+
 “function nightModeOff(){\ n”+
 “Android.printLogInfo('nightModeOff()'); \ n”+
 “isNightMode = false; \ n”+
 “var i = 0; \ n”+
 “var frame; \ n”+
 “while(frame = window.reader.dom.find('component',i ++)){\ n”+
 “frame.contentDocument.body.style.backgroundColor ='#ECECEC'; \ n”+
 “frame.contentDocument.body.style.color ='#1F1F1F'; \ n”+
 “} \ n”+
 “} \ n”+
 “\ n”+
 “function turnPage(pageNumber){\ n”+
 “Android.printLogInfo('turnPage('+ pageNumber +')'); \ n”+
 “window.reader.moveTo({page:pageNumber}); \ n”+
 “} \ n”+
 “\ n”+
 “function savePercents(){\ n”+
 “Android.printLogInfo('savePercents()'); \ n”+
 “Android.savePercents(window.reader.getPlace()。percentAtTopOfPage()); \ n”+
 “} \ n”+
 “\ n”+
 “function moveToPercents(percent){\ n”+
 “Android.printLogInfo('moveToPercents('+ percent +')'); \ n”+
 “turnPage(window.reader.getPlace()。pageAtPercentageThrough(percent)); \ n”+
 “} \ n”+
 “\ n”+
 “function listenFor(evtName){\ n”+
 “Monocle.Events.listen('reader',evtName,report); \ n”+
 “} \ n”+
 “\ n”+
 “功能报告(evt){\ n”+
 “switch(evt.type){\ n”+
 “case'monocle:loaded':\ n”+
 “Android.calculateSeekBar(window.reader.getPlace()。properties.component.lastPageNumber()); \ n”+
 “休息; \ n”+
 “\ n”+
 “案例'单片眼镜:转':\ n”+
 “Android.updatePagesCounter(window.reader.getPlace()。pageNumber()); \ n”+
 “休息; \ n”+
 “\ n”+
 “案例'单片眼镜:重新计算':\ n”+
 “if(isNightMode)nightModeOn(); \ n”+
 “if(isFirstLoading){isFirstLoading = false; turnPage(startPageNumber);} \ n”+
 “Android.calculateSeekBar(window.reader.getPlace()。properties.component.lastPageNumber()); \ n”+
 “Android.updatePagesCounter(window.reader.getPlace()。pageNumber()); \ n”+
 “休息; \ n”+
 “} \ n”+
 “} \ n”+
 “\ n”+
 “function init(){\ n”+
 “var options = {\ n”+ +
 “鳍状肢:Monocle.Flippers.Slider \ n”+
 “} \ n”+
 “\ n”+
 “listenFor('monocle:turn'); \ n”+
 “listenFor('monocle:loaded'); \ n”+
 “listenFor('monocle:recalculated'); \ n”+
 “\ n”+
 “window.reader = Monocle.Reader('reader',null,options); \ n”+
 “} \ n”+
 “\ n”+
 “Monocle.Events.listen(window,'load',init); \ n”+
 “< /脚本> \ n”);
 newCode = newCode.concat(code.substring(code.indexOf(“< head>”)+ 6,code.indexOf(“< body>”)+ 6));
 newCode = newCode.concat(“< div id = \”reader \“>”);
 newCode = newCode.concat(code.substring(code.indexOf(“< body>”)+ 6,code.indexOf(“< / body>”)));
 newCode = newCode.concat(“< / div>”);
 newCode = newCode.concat(code.substring(code.indexOf(“< / body>”)));
返回newCode;
}



 完成此任务后,WebView会加载它:

&# xA; contentView.loadDataWithBaseURL(baseUrl,prepareCode(code),“text / html”,“UTF-8”,null);


 
 我的结果是什么?我在Android 4.x和5.x的六台设备上测试了这个应用程序。在Android 4.x上,整个内容都正确显示,但在Android 5.x上我看到一个空白页面。我应该注意到xhtml代码被加载了,因为我可以通过JS接口调用JS函数并获得它的结果。


有没有关于修复这个bug的方法的想法?


提前致谢!