Android-webview supports zooming and hiding annoying zoom control bars

Source: Internet
Author: User

Reprint please indicate the source:
http://blog.csdn.net/hanhailong726188/article/details/46717621
This article is from "Dragon's Blog"

Previously reproduced Android WebView related articles, Android WebView development issues and optimization summary, here to explain the implementation of webview scaling and hide the zoom control bar encountered problems and pits

To support scaling, be sure to support JavaScript first, adding the following code:

        //支持JS        WebSettings settings = mWebView.getSettings();        settings.setJavaScriptEnabled(true);

The point is, if you want to support scaling, add the following code support

        //支持屏幕缩放        settings.setSupportZoom(true);        settings.setBuiltInZoomControls(true);

where Settings.setbuiltinzoomcontrols (true) must be added, otherwise scaling does not work, the author has been in this pit

through the above code, you can achieve webview scaling, the code is very simple, but there is a problem of egg pain, that is, when you zoom in the lower right corner of the webview will appear a nasty zoom control bar, as shown:

How do we get rid of it, after Android3.0 the implementation is very simple, add a line of code, the following code:

        //不显示webview缩放按钮        settings.setDisplayZoomControls(false);

In fact, this article should end here, but some students ask, you this only compatible to 3.0 or more versions, if I am compatible with the 2.x version? I want to say is, in fact, there is no need to be compatible to the 2.x version, 2.x of users, more than 4.x users have accounted for more than 94%, the following look at the Androidstudio statistics of each platform version of the user usage ratio:

So We do not need to be compatible with 2.x, just like iOS development is only compatible to IOS7 version of OK, but some students said, I have obsessive-compulsive disorder, I would like to be compatible to the 2.x version, in fact, the solution is available on the Internet, where the knowledge of Java reflection, through reflection to obtain a private property control Mzoombuttonscontro Ller, and then Setvisibility (View.gone) hidden on it, the following code to implement:

 Public void Setzoomcontrolgone(View view)        {Class ClassType; Field field;Try{ClassType = Webview.class; field = Classtype.getdeclaredfield ("Mzoombuttonscontroller"); Field.setaccessible (true); Zoombuttonscontroller Mzoombuttonscontroller =NewZoombuttonscontroller (view); Mzoombuttonscontroller.getzoomcontrols (). setvisibility (View.gone);Try{field.Set(view, Mzoombuttonscontroller); }Catch(IllegalArgumentException e)            {E.printstacktrace (); }Catch(Illegalaccessexception e)            {E.printstacktrace (); }        }Catch(SecurityException e)        {E.printstacktrace (); }Catch(Nosuchfieldexception e)        {E.printstacktrace (); }     }

After the above reflection code, you can achieve compatible with 2.x version of the hidden Zoom control bar, thank you!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-webview supports zooming and hiding annoying zoom control bars

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.