我正在开发一个android应用程序,需要在其中加载一个URL,该URL包含web视图中的印地语字体。
我使用了以下代码:
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setDefaultTextEncodingName("utf-8");
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(url);
该代码在大多数最新设备中运行良好,并正确显示印地语内容。但在Android 2.2、2.3或其他更低版本中,它显示的是方框,而不是印地语字符
我如何支持为非英语测试启用web视图,以便我的应用程序可以在所有设备上运行
提前感谢。。。
尝试以下链接:点击此处
private boolean copyFile(Context context,String fileName) {
boolean status = false;
try {
FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
InputStream in = context.getAssets().open(fileName);
// Transfer bytes from the input file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Close the streams
out.close();
in.close();
status = true;
} catch (Exception e) {
System.out.println("Exception in copyFile:: "+e.getMessage());
status = false;
}
System.out.println("copyFile Status:: "+status);
return status;
}
3.您只需要调用上述函数一次(您必须为此找到一些逻辑)。
copyFile(getContext(), "myfont.ttf");
4.使用以下代码为您的网络视图设置值。这里我使用CSS来设置字体。
private String getHtmlData(Context context, String data){
String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ context.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style></head>";
String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;
return htmlData;
}
5.您可以在下面调用上述函数
webview.loadDataWithBaseURL(null,getHtmlData(activity,htmlData),"text.html","utf-8","about:black");
我也为此挣扎了一天,但最终找到了解决方案。
只要加上这两行代码,就可以工作了
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");