WebView does not reduce height when compressing content

I came across the same question here . Basically I have text-webview-text in a vertical linear layout, as shown below:

__________ | Textview | |__________| | WebView | <-can zoom |__________| | TextView | |__________| 

The web browser has a zoom zoom, when the content zooms in on the web browser, it will be adjusted to a larger size and click on the text image:

  __________ | Textview | |__________| | | | | | WebView | | | |__________| | TextView | |__________| 

So far, everything is in order. The problem is that when I zoom in to a smaller size, the contents inside the webview are compressed, but the webview itslelf is still the same size, leaving a lot of empty space:

  __________ | Textview | |__________| | WebView | | | | | | | |__________| | TextView | |__________| 

My problem is also similar to this one . The invalidate () call will not work, and I don't want to recreate the fragment without disrupting the activity (I really don't know how to do this). My question is: how can I update / resize the height of the web browser when I zoom in from larger to smaller ? (NOTE: I am adding all the views dynamically, so please show me how I can solve it programmatically. Thanks)

This strange problem does not exist in Froyo 2.2, but it is a problem with an error in the real device JB 4.2.2.

+4
source share
1 answer

Add the code snippet as below, it will set the height of the webview when the page loads

 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"); } @JavascriptInterface public void resize(final float height) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { ViewGroup.LayoutParams lp = webView.getLayoutParams(); lp.width = getResources().getDisplayMetrics().widthPixels; lp.height = (int) (height * getResources().getDisplayMetrics().density); webView.setLayoutParams(lp); } }); } 

Changed from solution to https://capdroid.wordpress.com/2014/08/07/resizing-webview-to-match-the-content-size/

0
source

Source: https://habr.com/ru/post/1483094/


All Articles