在 android 网页视图中折叠 html 加载
Collapse html load in android Webview
我有 Html 内容显示崩溃 window。但这未加载到 Android WebView Section.Is 仅显示白屏。
我的 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;", "");
网上的截图是这样的
您很可能遇到过视图图层类型的问题。试试这个代码:
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);
}
并尝试在清单中设置硬件加速:
android:hardwareAccelerated="true"
答案已经在here
中解释过了
只需将加载数据函数替换为
webview.loadDataWithBaseURL(null,title,"text/html", "utf-8", null);
我有 Html 内容显示崩溃 window。但这未加载到 Android WebView Section.Is 仅显示白屏。
我的 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;", "");
网上的截图是这样的
您很可能遇到过视图图层类型的问题。试试这个代码:
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);
}
并尝试在清单中设置硬件加速:
android:hardwareAccelerated="true"
答案已经在here
中解释过了只需将加载数据函数替换为
webview.loadDataWithBaseURL(null,title,"text/html", "utf-8", null);