ScrollView with WebView, the ultimate solution for setting the WebView font size is to leave white below.

Source: Internet
Author: User

When ScrollView nested webview, if you need to call WebView settextsise, there will be a large gap in the bottom, tried a lot of methods do not work, inadvertently found a solution, incredibly can, I was drunk, I didn't think of it at the beginning. Okay, here's the code.

First write a method to reset the webview height

@JavascriptInterfacepublic void Resize (final float height) {    MyActivity.this.runOnUiThread (new Runnable () {        @ Override public        Void Run () {            webview.setlayoutparams (new Linearlayout.layoutparams (). Getresources (). Getdisplaymetrics (). Widthpixels, (int) (Height * getresources (). Getdisplaymetrics (). density)));        }    );

Then inject JS into your webview to dynamically get the height of this HTML content.

private void Setupwebview () {    webview.getsettings (). Setjavascriptenabled (true);    Webview.setwebviewclient (New Webviewclient () {        @Override public        void onpagefinished (webView view, String URL) {            Webview.loadurl ("Javascript:MyApp.resize (Document.body.getBoundingClientRect (). Height)");            super.onpagefinished (view, URL);        }    );    Webview.addjavascriptinterface (This, "MyApp");}

When you call settextsize each time, you only need to

Webview.loadurl ("Javascript:MyApp.resize (Document.body.getBoundingClientRect (). Height)");
Re-call this sentence will be OK.

ScrollView with WebView, the ultimate solution for setting the WebView font size is to leave white below.

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.