Webview未在Android中以正确的格式显示文本

时间:2018-09-09 12:32:25

标签: android webview

我正在将assests文件夹中的html文件显示到android中的Webview中。它显示不正确。这是webview enter image description here的图像,但是当我在crome broswer中打开显示正确的html文件时。这是浏览器的图像

enter image description here

下面是我的代码

 WebView webView = findViewById(R.id.web_view);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("file:///android_asset/demo.html");

下面是包含html文件的代码

<p class=MsoListParagraphCxSpLast style='margin-top:0in;margin-right:0in;
margin-bottom:0in;margin-left:13.5pt;margin-bottom:.0001pt;text-align:justify;
text-justify:inter-ideograph;text-indent:-13.5pt;line-height:normal'><b><span
style='font-size:10.0pt;font-family:"Times New Roman","serif"'>83-</span></b><span
style='font-size:10.0pt;font-family:"Times New Roman","serif"'>I</span><span
style='font-size:12.0pt;font-family:"Kruti Dev 010"'> vkSj </span><span
style='font-size:10.0pt;font-family:"Times New Roman","serif"'>O</span><span
style='font-size:12.0pt;font-family:"Kruti Dev 010"'> f=Hkqt </span><span
style='font-size:10.0pt;font-family:"Times New Roman","serif";color:black'>ABC</span><span
style='font-size:12.0pt;font-family:"Kruti Dev 010"'> ds Øe”k% var%dsUnz vkSj
ifjdsUnz gSaA c&lt;+kbZ xbZ js[kk </span><span style='font-size:10.0pt;
font-family:"Times New Roman","serif"'>AI, </span><span style='font-size:10.0pt;
font-family:"Matura MT Script Capitals"'>&#8710;</span><span style='font-size:
10.0pt;font-family:"Times New Roman","serif"'>ABC </span><span
style='font-size:12.0pt;font-family:"Kruti Dev 010"'>ds ifjo`Ùk dks fcUnq </span><span
style='font-size:10.0pt;font-family:"Times New Roman","serif"'>D</span><span
style='font-size:12.0pt;font-family:"Kruti Dev 010"'> ij izfrPNsfnr djrh gSA
;fn <span style='position:relative;top:5.0pt'><img width=121 height=23
src="img_1.png"></span>vkSj <span style='position:relative;
top:4.0pt'><img width=71 height=21 src="img_2.png"></span>rks<span
style='position:relative;top:13.0pt'><img width=53 height=40
src="img_3.png"></span></span><b><span style='font-size:10.0pt;
font-family:"Times New Roman","serif";color:black'>CGL Tier - I - 2013  Evening</span></b></p>

<p class=MsoNormal style='margin-top:0in;margin-right:0in;margin-bottom:0in;
margin-left:1.5in;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;
line-height:normal'><b><span style='font-size:10.0pt;font-family:"Times New Roman","serif"'>   
CGL Tier - I - 2013  Evening Set - 2 (21 - 04 - 2013)</span></b></p>

<p class=MsoListParagraph style='margin-top:0in;margin-right:0in;margin-bottom:
0in;margin-left:13.5pt;margin-bottom:.0001pt;text-align:justify;text-justify:
inter-ideograph;line-height:normal'><span style='font-size:10.0pt;font-family:
"Times New Roman","serif"'>(a) 3                                                  (b)
1</span></p>

<p class=MsoNormalCxSpFirst style='margin-top:0in;margin-right:0in;margin-bottom:
0in;margin-left:27.0pt;margin-bottom:.0001pt;text-indent:-13.5pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Times New Roman","serif"'>(c)
2

                                                  (d) 4</span></p>

我们如何像浏览器图像一样显示Web视图? 这也是包含html文件html file link

的链接

2 个答案:

答案 0 :(得分:0)

添加此webView.getSettings().setDefaultTextEncodingName("utf-8");以正确显示印地文字符

确保图像位于demo.html旁边的同一文件夹中的资产中

例如imags src之一是src="img_3.png",如果它位于文件夹assets/images中,则必须将其更改为src="images/img_3.png"

答案 1 :(得分:0)

尝试:(在资产文件夹中添加所需的字体之后)

  String head="<head><style>@font-face {font-family: 'Kruti Dev 010';src: url('file:///android_asset/krutidev.ttf');} " +
                "@font-face {font-family: 'Times New Roman';src: url('file:///android_asset/timesnewroman.ttf');}" +
                "@font-face {font-family: 'Matura MT Script Capitals';src: url('file:///android_asset/maturasc.ttf');}" +
                "body {font-family: 'Kruti Dev 010';}</style></head>";
        String HtmlString = "<html>"+head+"<body>"+containet()+"</body></html>";
        //myWebView.loadData(HtmlString, "contentType=text/html", "charset=UTF-8");
        myWebView.loadDataWithBaseURL("file:///android_asset/",HtmlString,"text/html","utf-8",null);

containet()返回HTML内容。