簡體   English   中英

Android 漢字 WebView

[英]Android Chinese characters in WebView

我有一個 html 代碼,我將其保存在home.txt文件中並將其放置在原始文件夾中。 現在我想在WebView中顯示相同的內容。 我為此使用了以下代碼。

homeWebview = (WebView) findViewById(R.id.homeWebview);
InputStream fileStream = getResources().openRawResource(R.raw.home); 
int fileLen = fileStream.available();

// Read the entire resource into a local byte buffer. 
byte[] fileBuffer = new byte[fileLen]; 
fileStream.read(fileBuffer);

fileStream.close();  
displayText = new String(fileBuffer);

//Display content.          
homeWebview.loadData(displayText, "text/html", "utf-8");

它工作正常。 現在我要在html中顯示一些漢字。 我在home.txt中添加了漢字。 這是 html 代碼:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Language</title>
<style type="text/css">
body {font-family: HelveticaExt-normal, Helvetica, Arial, sans-serif;margin:0;font-size:20px;color:#000000;}
.mainWrapper {margin:0;}
p {margin: 10px;}
p.bodytext {margin: 0 10px 20px;}
.headerText { background:#eeeeee; background: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#e0e0e0));border-top: 2px solid #000000;padding-bottom:7px;}
.headerText p {margin: 10px 10px 0 10px;}
a {color:#324f85;text-decoration:underline;}
.headerbar {border-top:2px solid #000000;border-bottom:2px solid #000000;height:34px;font-family:HelveticaExt-bold, Helvetica, Sans-serif;color:#fff;font-size:18px;padding-left:10px;line-height:34px;margin:0;
 background:-webkit-gradient(linear, left top, left bottom, from(#494e4f), to(#787a7c));
}
.navigationList {list-style-type:none;padding:0;margin:0;}
.navigationList li {height:60px;border-bottom:2px solid #e0e0e0;background-position:10px center;background-repeat:no-repeat;padding-left:60px;line-height:28px;
}
</style>
</head>

<body>
<div class="mainWrapper">
    <div class="headerText">
        <p歡迎您到泰國語言主持人!<a href="http://www.test.com">www.test.com</a> 欲了解更多信息!</p>
    </div>
</div>
</body>

現在它可以很好地顯示常用字符,但是所有中文字符都顯示為未知字符。 我的代碼中有什么問題?

使用webView.loadDataWithBaseURL (null, text, "text/html", "utf-8", null);

home.txt 文件不是 UTF-8 編碼的,我在記事本++中打開它並更改編碼。 現在它顯示正確。

我認為這一行的問題:

displayText = new String(fileBuffer);

你應該改為

displayText = new String(fileBuffer, "UTF-8");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM