UTF-8不在webview android中编码html

vya*_*yas 5 encoding android

我使用以下代码编码我的资产文件夹中的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"); 
Run Code Online (Sandbox Code Playgroud)

虽然它适用于其他角色,但它无法编码 - 因为它在Web视图上打印相同.请建议我做什么.

任何帮助将不胜感激.

Der*_*rzu 11

您需要将WebSettings默认文本编码设置为utf-8.并强制html标头charset到utf-8.Exmple:

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");
Run Code Online (Sandbox Code Playgroud)

这适用于android 2.2.1,4.0.4和4.1.2.

  • 使用"text/html; charset = utf-8","utf-8"为我工作.谢谢.(android 5.0.1) (3认同)

Par*_*ani 1

您可以尝试检查一下:

myWebVeiw.loadDataWithBaseURL("file:///android_asset/"+totaldays+".html", null, "text/html", "utf-8",null);
Run Code Online (Sandbox Code Playgroud)