Android WebView出現的亂碼問題

一、webview加載網頁<html>源碼亂碼問題html

1、webView.loadUrl();android

直接顯示網頁內容(單獨顯示網絡圖片),通常不會出現亂碼。web

2、webView.loadData(data, "text/html", "UTF-8");瀏覽器

loadData主要被設計用來裝載URI格式的數據,它不能經過網絡來加載內容。網上流傳的webview加載中文出現亂碼,多數是使用此方法。使用過程當中主要有兩個問題:網絡

(1)loadData不能加載圖片內容,若是想加載圖片內容或者得到更強大的Web支持建議使用更強大的loadDataWithBaseURL.ide

(2) 許多實用loadData方法的朋友都遇到顯示亂碼的問題,那是由於編碼器設置錯誤致使的。咱們知道String類型的數據主要是unicode編碼,而WebView通常爲了節省資源使用的是UTF-8編碼,因此咱們在loadData的時候要告訴方法怎樣轉碼。即要告訴它要將unicode編碼的內容轉成UTF-8編碼的內容。有些朋友雖然在loadData的時候設置了編碼方式,可是仍是顯示亂碼,這是由於還須要爲WebView的text編碼指定編碼方式。舉例以下:測試

WebView wv = (WebView)findViewById(R.id.webview) ;字體

wv.getSettings().setDefaultTextEncodingName(「UTF -8」) ;ui

wv.loadData(content, 「text/html」, 「UTF-8」) ;編碼

注意爲gb2312或gbk

(3).網頁說明編碼格式 

以上兩種方法是網上給的比較好的方法,可是我都試了下都沒有解決個人亂碼問題。 原來我是用LoadData方法來解析html的,可是聽說這是官方的一個BUG,不能用來解析中文。因此繞其道而行之,採用loadDataWithBaseURL的方法,其中codeingType設置爲utf-8就OK了。三、loadDataWithBaseURL若是單純顯示文字的話能夠寫webView.loadDataWithBaseURL(null, string, "text/html", "utf-8", null);

若是要顯示圖片能夠寫webView.loadDataWithBaseURL(baseUrl, string, "text/html", "utf-8", null);

其中baseUrl爲你存儲照片的路徑,好比:

二、webview背景透明:

在xml文件中設置android:background無效。

    <WebView

                android:id="@+id/big_data_detail_content_textview"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:textColor="#fff"

                android:background="@drawable/color_transparent"

                android:textSize="14dp" />

解決辦法:

webview.setBackgroundColor(0); // 設置背景透明

  1. 1.在2.X的平臺下,通常設置webview背景爲透明的方法以下:  

  2.   

  3. wvContent.setBackgroundColor(0);    

  4. 這個相信你們測試後都是沒有問題的!  

  5.   

  6. 2.但當程序在4.0上使用時,發現竟然這種設置方法沒法,即便經過上面設置背景爲0,照樣顯示出原來默認的白色背景(在我本身的話huawei U9500)中測試是部分會顯示白色,有些界面的渲染仍是成功的。這中現象是android4.0後,系統內部的加速器作了改變  

  7. 詳細內容參考[color=red]http://blog.chenming.info/blog/2012/09/18/android-hardware-accel/[/color] 一篇很好的文章,我也是在這裏找到問題解決的方法!  

  8.   

  9. 其實我按照方法,在WebView控件中是硬件加速器失效即  android:layerType="software",即便這樣,我本身的機器測試仍是出現部分WebView是白色背景!  

  10. 最後我在該Activity中註冊了 android:hardwareAccelerated="false" 就能夠了!

 

二:webView加載本地字體包:

purchase_detail_content_webview.setBackgroundColor(0); // 設置背景色

String detaliContent = "<html><head><style> @font-face {font-family: MyFont; src: url('file:///android_asset/fonts/hunda.TTF');}  div{text-indent: 2em;color:white;font-size:15;font-family:MyFont}</style></head><div>" + CONTENT + "</div><html>"; //asset/fonts/hunda.TTF:本地字體包文件;CONTENT :服務端發送的內容

purchase_detail_content_webview.loadDataWithBaseURL(null, detaliContent, "text/html", "utf-8", null);

原文地址:http://wptrafficanalyzer.in/blog/android-using-custom-ttf-fonts-in-webview/ 

3、webview和js腳本語言交互

http://zhidao.baidu.com/link?url=7u5Jw1vq1YBlhMpYoZwEx7bzvnrl05v-G6FFCMUeHyjDl5Wd6Dh3w1zBYghvGehlL0BV_RxtcvoFvjcZtZ7sUm60pg6oOxm08UDynB3wXeS 

4、返回上一個頁面

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();// 返回上一個頁面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

不調用第三方瀏覽器,在本webview內部跳轉

mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

webSettings.setJavaScriptEnabled(true);         // 支持縮放         webSettings.setBuiltInZoomControls(true);         webSettings.setSupportZoom(true);         // 隱藏zoom縮放按鈕         // webSettings.setDisplayZoomControls(false);         // 自適應屏幕         webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

相關文章
相關標籤/搜索