WebView wrap-content
Is it possible to load a URL in a WebView and resize it to fit the screen? I mean I want to make the WebPage small so that the user doesn't need to scroll. Is this 开发者_Go百科possible?
You can also try setting an layout algorithm for the same.It worked for me
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
Set the initial scale to 1. It then zooms in to fill the screen. Haven't tested how this works on really tall web sites (blog posts with lots of comments for example) but it's working on a Droid X and an HTC Aria with a standard web page.
WebView.setInitialScale(1);
I'll continue testing with different web pages but so far this is the best solution I've got for this issue.
EDIT: Tested with an obscenely long web page on both Droid X and Aria and got positive results on both. Setting the initial scale to 0 seems to turn the whole thing off resulting in initial scale being 100. I'm happy with these results, hope this helps others struggling with this issue.
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
webview_settings.setLoadWithOverviewMode(true);
webview_settings.setUseWideViewPort(true);
webview_settings.setBuiltInZoomControls(true);
Have you tried just calling WebView.zoomOut()
repeatedly until it returns false
?
Also, I would note that this technique you want to use really relies on the web page being quite short. If the page is long and you zoom out to fit it entirely on screen, the user is going to need very good eye sight! ;)
I was able to contain the entire page in my view by doing the following. First set the width and height to wrap_content. Then setting the initial scale to one like this:
WebView webview = (WebView) currentActivity.findView(R.layout.web_view, R.id.webview);
webview.setInitialScale(1);
I found that I could get this to keep the width to the minimum necessary to display a readable font and then scroll along the y coordinate every time by doing the following. First set both the height and width to wrap_content. Then set the zoom density to far in your code like this:
WebView webview = (WebView) currentActivity.findView(R.layout.web_view, R.id.webview);
webview.getSettings().setDefaultZoom(ZoomDensity.FAR);
This is probably the better solution most of the time. Consider how small the font would be if you displayed the following web page in its entirety:
webview.setInitialScale(number);
where the higher 'number' is, the more zoomed in your page will be. start with lower numbers ( like 10) and adjust to taste
Use this code for resize your webview according to screen .
setScrollContainer(false);
I got this to work in a dialog using WRAP_CONTENT for both dimensions of the WebView, unfortunately when the web page becomes too large the webview pushes all my other widgets off the screen.
Android's layout system is pretty damn unintuitive!
As mentioned, zooming out too much is not something the user would like cause it'll reduce the legibility of the content.
I would suggest trying to set the zoom controls, however, I don't know if it'll work in 2.1.
webview.getSettings().setBuiltInZoomControls(true);
Hope this solves the problem as you now are giving user the control to change the zoom level.
精彩评论