Android 修改h5数据样式及WebView的加速处理

1.创建一个工具类

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) {

        // 设置webview不允许竖直滚动
        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", "");

    }

使用直接调用就可以了,

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

3.也可以通过WebSetting对WeiView进行一些修改

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里对WebView开启X5内核进行加速处理

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

            //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
            @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内核初始化接口
        Log.d(LogTag, "APPAplication onCreate finish...");
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容