Android WebView 包装内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1991219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:05:21  来源:igfitidea点击:

WebView wrap-content

androidwebview

提问by JaVadid

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 possible?

是否可以在 WebView 中加载 URL 并调整其大小以适合屏幕?我的意思是我想让网页变小,这样用户就不需要滚动了。这可能吗?

采纳答案by JaVadid

@user289751, yal??n

@ user289751,是吗??n

well friends as "Christopher" mentioned, zooming out too much is not something the user would like coz it'll reduce the legibility of the content shown...

正如“克里斯托弗”所提到的,朋友们,用户不希望缩小太多,因为它会降低所显示内容的易读性......

rather i would like to suggest a alternate... its better u try setting the zoom controls... don know if they'll work in 2.1 or not...

相反,我想建议一个替代方案...最好尝试设置缩放控件...不知道它们是否可以在 2.1 中工作...

webview.getSettings().setBuiltInZoomControls(true);

hope this solves ur problem... coz now u r giving user the control to change the zoom level...

希望这能解决你的问题......因为现在你给了用户改变缩放级别的控制......

回答by DeRagan

You can also try setting an layout algorithm for the same.It worked for me

您也可以尝试设置相同的布局算法。它对我有用

webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

回答by cesarislaw

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.

将初始比例设置为 1。然后放大以填满屏幕。还没有在非常高的网站上测试它是如何工作的(例如,有很多评论的博客文章),但它在 Droid X 和带有标准网页的 HTC Aria 上工作。

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.

编辑:在 Droid X 和 Aria 上用一个很长的网页进行了测试,并在两者上都得到了积极的结果。将初始比例设置为 0 似乎将整个事情关闭,导致初始比例为 100。我对这些结果很满意,希望这可以帮助其他人在这个问题上苦苦挣扎。

回答by Sujith Mohan

wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);

回答by exiquio

webview_settings.setLoadWithOverviewMode(true);
webview_settings.setUseWideViewPort(true);
webview_settings.setBuiltInZoomControls(true);

回答by Danny Remington - OMS

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:

通过执行以下操作,我能够在我的视图中包含整个页面。首先将宽度和高度设置为 wrap_content。然后将初始比例设置为这样:

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:

我发现我可以通过执行以下操作将宽度保持在显示可读字体所需的最小值,然后每次沿 y 坐标滚动。首先将高度和宽度都设置为 wrap_content。然后在您的代码中将缩放密度设置为远,如下所示:

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:

大多数情况下,这可能是更好的解决方案。如果您完整地显示以下网页,请考虑字体的大小:

回答by Christopher Orr

Have you tried just calling WebView.zoomOut()repeatedly until it returns false?

您是否尝试过WebView.zoomOut()重复调用直到它返回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! ;)

另外,我要指出的是,您要使用的这种技术实际上依赖于非常短的网页。如果页面很长并且您将其缩小以完全适合屏幕,则用户将需要非常好的视力!;)

回答by smoothwalsh

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

“数字”越高,页面放大得越多。从较低的数字(如 10)开始并根据口味进行调整

回答by Rishabh Agrawal

Use this code for resize your webview according to screen .

使用此代码根据 screen 调整 webview 的大小。

setScrollContainer(false);

setScrollContainer(false);

回答by Timmmm

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.

我让它在一个对话框中使用 WRAP_CONTENT 来处理 WebView 的两个维度,不幸的是,当网页变得太大时,webview 会将我所有的其他小部件从屏幕上推开。

Android's layout system is pretty damn unintuitive!

Android 的布局系统非常不直观!