在 webview 中拟合网页内容(Android) [英] Fitting webpage contents inside a webview (Android)

查看:20
本文介绍了在 webview 中拟合网页内容(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题.

我正在尝试拉伸 webview 的内容,以便可以看到其中的整个网页.即没有滚动.我已经浏览了文档,但除了缩放控件和 setinitialscale 之外找不到任何方法.

I'm trying to stretch the contents of a webview so that the entire webpage within is visible. i.e no scrolling. I've looked through the docs but cant find any method asides from zoom controls and setinitialscale.

在这种情况下 setinitialscale 的问题在于它对不同站点的工作方式不同.

The problem with setinitialscale in this case is that it works differently for different sites.

示例:1:wikipedia 将按预期加载并放大足够远.2:Google 只会显示页面的中心.

Example: 1:wikipedia will load as expected with the zoom far enough out. 2:Google however will only show the center of the page.

这是我的代码片段

    test1 = (WebView) findViewById(R.id.webview_test_1);
    test2 = (WebView) findViewById(R.id.webview_test_2);
    test3 = (WebView) findViewById(R.id.webview_test_3);

    test1.getSettings().setJavaScriptEnabled(true);
    test2.getSettings().setJavaScriptEnabled(true);
    test3.getSettings().setJavaScriptEnabled(true);

    test1.setInitialScale(12);
    test2.setInitialScale(12);
    test3.setInitialScale(12);

    test1.loadUrl("http://developer.android.com/resources/tutorials/views/hello-tablelayout.html");
    test2.loadUrl("http://www.wikipedia.org/");
    test3.loadUrl("http://www.google.com");

<小时>

test1.getSettings().setDefaultZoom(ZoomDensity.FAR);

这似乎是我尝试做的事情的替代方案,但我无法将其放大到足够远.

This seems to be an alternative to what I'm trying to do but I cant get it to zoom far enough.

推荐答案

因此人们可以将其用作将来的教程.

So People can use this as a tutorial in the future.

有很多方法可以处理 android 和适配页面的缩放.有时它可能会非常喜怒无常,有些方法比其他方法更有效.

There are a bunch of ways to handle zooms in android and fitting pages. It can be pretty temperamental at times and some methods work better than others.

对于大多数人来说,只需使用这个:

For most people, Just use this:

WebView x;

x.setInitialScale(1);

这是可能的最远变焦.但对于某些网站来说,它只是看起来很丑.

This is the furtheset zoom possible. But for some sites it just looks pure UGLY.

这是我找到的第二个版本

This was the second version I found

test1.getSettings().setDefaultZoom(ZoomDensity.FAR);

这是一个很好的多面手,似乎只是缩小到足够多但仍然不是我想要的.

Thats a nice all rounder than seems to just zoom out far enough for a lot but still not what I was looking for.

现在这是我的最终解决方案.

And now heres the final solution I have.

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

基本上,在另一个类似的问题中回答了这些问题.

Basically what these do is answered in another question like this.

 setLoadWithOverviewMode(true) 

加载 WebView 完全缩小

Loads the WebView completely zoomed out

setUseWideViewPort(true)

使 Webview 具有正常视口(例如普通桌面浏览器),而当为 false 时,Webview 将具有一个受其自身尺寸约束的视口(因此,如果 Webview 为 50px*50px,则视口将具有相同的大小)

Makes the Webview have a normal viewport (such as a normal desktop browser), while when false the webview will have a viewport constrained to it's own dimensions (so if the webview is 50px*50px the viewport will be the same size)

这篇关于在 webview 中拟合网页内容(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆