如何在Android应用程序中使用Web视图显示印地语(非英语)网页

时间:2013-08-08 06:59:46

标签: android android-layout android-widget android-webview android-websettings

我正在开发一个Android应用程序,我需要在Web视图中加载包含印地语字体的URL。

我使用了以下代码:

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或其他较低版本中,它显示的是盒子而非印地语字符。

如何支持启用非英语测试的网络视图,以便我的应用程序可以在所有设备上运行。

提前致谢...

2 个答案:

答案 0 :(得分:1)

尝试以下链接:Click here

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.使用以下代码为webview设置值。这里我使用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:blank”);

答案 1 :(得分:0)

我也为此苦了一天,但终于找到了解决方法。

只需添加这两行代码即可使用

 WebSettings webSettings = webView.getSettings();
 webSettings.setDefaultTextEncodingName("utf-8");