5

我正在使用以下代码对我的资产文件夹中的 html 文件进行编码。我在这里浏览了各种链接,但没有成功。这是我的一段代码。

 WebSettings settings = myWebVeiw.getSettings();
    //settings.setJavaScriptEnabled(true);
    //myWebVeiw.getSettings().setJavaScriptEnabled(true);
    settings.setDefaultTextEncodingName("utf-8");
    //settings.setDefaultTextEncodingName("ISO-8859-1");

    myWebVeiw.loadUrl("file:///android_asset/"+totaldays+".html"); 

虽然它适用于其他字符,但它无法编码 - 因为它在 Web 视图上打印相同。请建议我该怎么做。

任何帮助将不胜感激。

4

3 回答 3

11

您需要将 WebSettings 默认文本编码设置为 utf-8。并在将 html 标头字符集强制为 utf-8 之后。示例:

String htmlText = "<html> your page here </html>";
WebView web = (WebView) findViewById(R.id.webview); // get your webview from the layout
WebSettings settings = web.getSettings();
settings.setDefaultTextEncodingName("utf-8");
web.loadData(htmlText, "text/html; charset=utf-8", "utf-8");

这适用于我在 android 2.2.1、4.0.4 和 4.1.2 上。

于 2013-03-24T22:41:37.940 回答
1

你能试着检查一下:

myWebVeiw.loadDataWithBaseURL("file:///android_asset/"+totaldays+".html", null, "text/html", "utf-8",null);
于 2013-01-23T07:16:03.500 回答
0

对webView不太熟悉。但是浏览器解析一个html字符集编码如下: 1.first charset .so请检查totaldays.html <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 2.根据内容。

于 2013-01-23T07:17:41.887 回答