首页 > 解决方案 > 在android Webview中折叠html加载

问题描述

我有 Html 内容显示折叠窗口。但这不是在 Android WebView 部分中加载的。仅显示白屏。

我的 WebView 代码是

 WebView webView = (WebView) rootView.findViewById(R.id.webview);
            webView.getSettings().setAllowFileAccess(true);
            webView.getSettings().setJavaScriptEnabled(true);
           // webView.getSettings().setBuiltInZoomControls(true);
           // webView.setInitialScale(1);
            webView.getSettings().setDomStorageEnabled(true);
            webView.getSettings().setLoadsImagesAutomatically(true);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView.setWebChromeClient(new WebChromeClient());
            webView.getSettings().setSupportMultipleWindows(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

title="<head><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">"+
                    "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js\"></script>"+
                    "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></head>";
            title= title+"<div class=\"panel-group\" id=\"accordion\"><div class=\"panel panel-default\"><div class=\"panel-heading\"><h4 class=\"panel-title\">" +
                    "<a data-toggle=\"collapse\" data-parent=\"#accordion\" href=\"#collapseOne\">1. What is HTML?</a></h4></div><div id=\"collapseOne\" class=\"panel-collapse collapse in\"><div class=\"panel-body\">" +
                    "<p>HTML stands for HyperText Markup Language. HTML is the standard markup language for describing the structure of web pages. <a href=\"https://www.tutorialrepublic.com/html-tutorial/\" target=\"_blank\">Learn more.</a></p>" +
  " </div></div></div></div>";
webView.loadData(title, "text/html;", "");

网络中的屏幕截图看起来像 在此处输入图像描述

标签: androidandroid-webviewloaddata

解决方案


您很可能遇到过视图层类型的问题。试试这个代码:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    webSettings.setMixedContentMode(0);
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

并尝试在 Manifest 中设置硬件加速:

android:hardwareAccelerated="true"

推荐阅读