Some simple uses of WebView in Android
Always wanted to write a simple application about the WebView control, there is no time, this is also squeeze time to write, inside some of the basic knowledge and so have time to update the explanation, today's first project out to do some simple introduction, too much content can see my source code, have been uploaded to the GitHub.
The following is an effect chart for the project:
Application is to use the MVP design mode, the model is not very familiar with the first Google, otherwise the project will look dizzy, although my code is very concise.
For the MVP can take a thought to see the source code, that is, activity (or other components) through the Xxpresenter to get the data, get the data in Xxpresenter reuse Xxiview (this is an interface) to update the data, So the activity (or other component) Inherits Xxiview this interface to update the UI.
In fact, WebView only use some simple, in-depth application of this article will be updated!
which
Set some scaling function points of the WebView
Webview.setscrollbarstyle (webview.scrollbars_outside_overlay);
Webview.sethorizontalscrollbarenabled (false);
Webview.getsettings (). Setsupportzoom (true);
Set the WebView to zoom in and out
webview.getsettings (). Setbuiltinzoomcontrols (true);
Webview.setinitialscale ();
Webview.sethorizontalscrollbaroverlay (true);
WebView double click to become large, and then double-click to become smaller, when manually enlarged, double-click can be restored to the original size
//webview.getsettings (). Setusewideviewport (true);
Increase the priority of the rendering
webview.getsettings (). setrenderpriority (Renderpriority.high);
Allow JS to execute
webview.getsettings (). Setjavascriptenabled (true);
Put the picture loaded at the end to load the render
//webview.getsettings (). Setblocknetworkimage (true);
Use WebView to display the string as HTML
//webview.loaddatawithbaseurl ("fake://not/needed", <p>zzz</p>, "text /html "," Utf-8 "," "");
In the case of the same resolution, the screen density is different, the automatic adaptation page:
Interacting with native:
Mwebview.addjavascriptinterface (New Webappinterface (Customview.getcontext ()), "Android");
public class Webappinterface {context
mcontext;
/** instantiate the interface and set the context * *
webappinterface (context c) {
mcontext = c;
}
/** show a toast from the Web page *
/@JavascriptInterface public
void Showtoast (String toast) {
//click WEBV Iew loaded HTML fragment allows the application to pop a toast
}
This sets the night mode for WebView:
Write HTML tags to html page "<div class=" Night ">"
public static string Buildhtmlwithcss (string html, string[] cssURLs, Boolean isnightmode) {
StringBuilder result = new StringBuilder ();
for (String cssurl:cssurls) {
result.append (String.Format (Css_link_pattern, Cssurl));
if (isnightmode) {
result.append (night_div_tag_start);
}
Result.append (Html.replace (Div_image_place_holder, div_image_place_holder_ignored));
if (isnightmode) {
result.append (night_div_tag_end);
}
return result.tostring ();
}
The project has yet to be improved, and when the time is up, the new!!!!
Project Source GitHub