Android 修改h5數(shù)據(jù)樣式及WebView的加速處理

1.創(chuàng)建一個(gè)工具類

public class WebFont {

    public static void showWebFont(WebView myWebView, String content) {

        showWebFont(myWebView, content, 16);

    }

    public static void showWebFont(WebView myWebView, String content,
                                   int fontSize) {

        showWebFont(myWebView, content, fontSize, "#323232");

    }

    public static void showWebFont(WebView myWebView, String content,
                                   int fontSize, String fontColor) {

        showWebFont(myWebView, content, fontSize, fontColor, "#fff8f7");

    }

    public static void showWebFont(WebView myWebView, String content,
                                   int fontSize, String fontColor , String bg , String a) {

        showWebFont(myWebView, content, fontSize, fontColor, bg);

    }

    /**
     * 顯示文字
     *
     * @param myWebView
     *            顯示文字的webview
     * @param content
     *            顯示的文字
     * @param fontSize
     *            文字大小
     * @param fontColor
     *            文字顏色
     * @param backgroundColor
     *            顯示文字的背景
     */
    public static void showWebFont(WebView myWebView, String content,
                                   int fontSize, String fontColor, String backgroundColor) {

        // 設(shè)置webview不允許豎直滾動(dòng)
        myWebView.setVerticalScrollBarEnabled(false);
        //backgroundColor = "#fff8f7";

        myWebView.loadDataWithBaseURL("",
                "<![CDATA[<html> <head></head> <body style=\"background-color:"
                        + backgroundColor + ";text-align:justify;font-size:"
                        + fontSize + "px;color:" + fontColor
                        + ";\"> " + content
                        + " <p> </body></html>", "text/html", "utf-8", "");

    }

使用直接調(diào)用就可以了,

     WebFont webFont = new WebFont();
      webFont.showWebFont(mTongzhiNei,h5);

3.也可以通過WebSetting對(duì)WeiView進(jìn)行一些修改

WebSettings webSetting = mWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setSaveFormData(false);
        mWebView.setWebViewClient(mWebViewClient);

4.也可以在Application里對(duì)WebView開啟X5內(nèi)核進(jìn)行加速處理

QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            //x5內(nèi)核初始化完成的回調(diào),為true表示x5內(nèi)核加載成功,否則表示x5內(nèi)核加載失敗,會(huì)自動(dòng)切換到系統(tǒng)內(nèi)核。
            @Override
            public void onViewInitFinished(boolean arg0) {
                Log.d(LogTag, " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                Log.d(LogTag, "onCoreInitFinished...");
            }
        };

        QbSdk.initX5Environment(getApplicationContext(),  cb);  //x5內(nèi)核初始化接口
        Log.d(LogTag, "APPAplication onCreate finish...");
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容