请教高手:
在SDK文档中提示webview.loadData方法不支持中文解析,通常用WebView.loadDataWithBaseURL来解决。
我在一个小项目中使用了webview,在测试过程中发现,LoadData方法中文乱码,便换成了loadDataWithBaseURL方法,在android4.0.3(含)以下版本系统中是正常的(webview启用了划屏翻页),
有意思的是,我发现在android4.0.4版本上,loadDataWithBaseURL竟然在划屏翻页后不加载内容了,跟踪代码输出的内容,html字符串在到webview前都是正常的,唯独webview显示出了问题,问题如下:
1、使用loadData时,中文乱码,所有的系统都是,但划屏翻页是正常的。
2、若改为loadDataWithBaseURL,中文显示正常了,但划屏翻页了却不正常加载。
代码如下:
- String Temp="";
- Temp=""
- + "<style>font{font-size:45px;line-height:60px}body{padding:5px;width:100%;font-size:45px;color:white;line-height:60px;min-width:" + myValue.ScreenWidth + "px;"
- + "font-weight:normal;border:red 2px solid}a{color:white}img{width:100%;min-width:" + myValue.ScreenWidth + "px;border:green 0px solid}"
- + "div{width:100%;border:blue 3px solid;margin:5px;min-width:" + myValue.ScreenWidth + "px}p{width:100%;min-width:" + myValue.ScreenWidth + "px"
- + ";border:white 1px solid;padding:5px;height:auto}"
- + "</style>";
- Temp=Temp.replace("<div","<p");
- Temp=Temp.replace("</div>", "</p>");
- Temp=Temp + cursor.getString(2);//.replace("<p", "<div").replace("</p>", "</div>");
- WebView v=(WebView)findViewById(R.id.wv);
- v.getSettings().setSupportZoom(true);
- v.setBackgroundColor(Color.BLACK);
- //v.loadUrl("about:blank");
-
- v.clearCache(true);
- v.getSettings().setDefaultTextEncodingName("utf-8");
- Log.i("ROBACK", "Default Encoding = " + v.getSettings().getDefaultTextEncodingName());
- //v.loadData(new String(Temp).replaceAll("#", "%23").replaceAll("%",
- // "%25").replaceAll("\'", "%27"), "text/html", "utf-8");
- //v.loadData(Temp, "text/html", "utf-8");
- v.loadDataWithBaseURL(null,Temp, "text/html", "utf-8",null);
- this.setTitle(cursor.getString(1));
-
- v.setWebViewClient(new WebViewClient(){
- public void onReceivedError(WebView view,int errorCode,String description,String failingUrl)
- {
- Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show();
- }
- });
- v.setWebChromeClient(new WebChromeClient(){
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- WebView v=(WebView)findViewById(R.id.wv);
- //Toast.makeText(mContext, v.getUrl() + newProgress +"加载中...", Toast.LENGTH_SHORT).show();
- ProgressBar s=(ProgressBar)findViewById(R.id.progressBar1);
- s.setMax(100);
- s.setProgress(newProgress);
- if(newProgress==100){
- v.setVisibility(0);
- //Toast.makeText(mContext, "加载结束...", Toast.LENGTH_SHORT).show();
- }else{
- v.setVisibility(8);
- //Toast.makeText(mContext, "加载中...", Toast.LENGTH_SHORT).show();
- }
- }
- });