Android WebView How to load HTML files under assets

Source: Internet
Author: User

Project requirements: Place HTML files and the images you use in the assets/html/directory. And then show it through WebView on the page.

Pay the code directly:

private void Readhtmlformassets () {
WebSettings websettings = Tipswebview.getsettings ();

Websettings.setloadwithoverviewmode (TRUE);
Websettings.setusewideviewport (TRUE);

Tipswebview.setbackgroundcolor (color.transparent); WebView background transparency effect, do not know why in the XML configuration can not be set?
Tipswebview.loadurl ("file:///android_asset/html/tips.htm");
}

The above is the commonly used WebView display settings, through the previous code can be successfully displayed HTML files, as long as the HTML code to ensure that the image is a relative address, and can be found correctly can display the image contained in the page. (In this case, the picture is in the same directory.) )

Display effect: Can zoom when clicked, Picture normal display. The initial display is scaled to a reasonable size. So the previous code is the common code base for the future.

Note: WebView default display effect is 100% px display, I want the effect is zoom to a reasonable size so that the content is all visible. So you can use the Setinitialscale to set the zoom, but different screen needs to set the same scale, and this parameter is not good in the XML set, so it is more troublesome. With the code above, the problem is solved.

I also experimented with Setinitialscale but on different screens sizes and Web page sizes that won ' t is as graceful as the Browsers scaling.

Android WebView How to load HTML files under assets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.