更新:如果您有更新的API(> 18),您可能需要查看此问题末尾的注释。
经过一段时间的努力,我find了解决方案!
解决方案不是缩放。 没有为html页面启用缩放,它不会影响html感知的大小。
我通过降低webview的宽度得到它:
ViewGroup.LayoutParams webviewLayoutParameters = mWebView.getLayoutParams(); webviewLayoutParameters.width = (int) (760); webviewLayoutParameters.height =(int) (webviewHeight); mWebView.setLayoutParams(webviewLayoutParameters);
然后,由于页面具有针对低于768px的屏幕尺寸而改变的样式,因此它看起来看起来不同。 好多了。
但后来我想扩大那个小的webview,以便它可以适合整个屏幕 。 为此,我使用了scaleX和scaleY 。
这是我完整的评论解决方案:
// First, get the current screen size Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int displayWidth = size.x; int displayHeight = size.y; //Then, set the amount you want to make your screen smaller. This means that it will comunicate to the HTML that its 20% smaller than it really is float amplification_ratio = (float) 0.20; // Shrink webview size ViewGroup.LayoutParams webviewLayoutParameters = mWebView.getLayoutParams(); webviewLayoutParameters.width = (int) (displayWidth*(1-amplification_ratio)); webviewLayoutParameters.height =(int) (displayHeight*(1-amplification_ratio)); mWebView.setLayoutParams(webviewLayoutParameters); // Now scale it the inverse ammount so it will take the full screen. mWebView.setScaleX(1/(1 - amplification_ratio)); mWebView.setScaleY(1/(1 - amplification_ratio));
它工作得很好。 我设法以改变屏幕大小的方式缩放它。 缩小和缩放将使图像质量稍低,但这不是什么大问题。
Obs :HTML页面是在bootstrap中创建的,只有bootstrap的-xs设备类。 所以我不得不模拟屏幕小于768px。
Obs2(更新):命名此过程
我在我的应用程序上实现了它,测试了它(模型和集成),现在它已经投入生产。 要创建函数和variables,我必须命名这个过程。 这样做就像增加webview像素的大小。 通过这种方式,webview仍将采用整个屏幕,但它的宽度(以像素为单位)会更小。
考虑到这一点,像pixelEnlargementFactor , pixelMagnificationRatio或pixelSizeMultiplicator这样的任何名称都是一个好名字。
更新:
警告:仅在特定情况下使用上述方法!
我有一个旧的API版本(15),并有一个RelativeLayout有很多选项。 那时, setInitialScale(120)没有按我的setInitialScale(120)工作。
现在我将我的API更改为19,将我的布局更改为更简单的FrameLayout ,选项更少。 现在setInitialScale(120)命令和mWebView.getSettings().setUseWideViewPort(false)完成了我以前用更少的代码实现的所有function。