WebView自适用屏幕

来源:互联网 发布:淘宝用户冻结无法登陆 编辑:程序博客网 时间:2024/06/10 01:16

可以通过设置LayoutAlgorithm的布局模式来控制WebView

SDK给的方法解释:Sets the underlying layout algorithm. This will cause a relayout of the WebView. The default is{@link LayoutAlgorithm#NARROW_COLUMNS}.

LayoutAlgorithm有三种模式可以选择:

1、LayoutAlgorithm.NARROW_COLUMNS 列宽尽可能的窄,但页面很大的话还是会超过手机的屏幕(默认方式)

2、LayoutAlgorithm.SINGLE_COLUMN 只有一列,宽度与手机屏宽度一样

3、LayoutAlgorithm.NORMAL 正常显示,WebView不作处理


可以设置mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);使H5页面适用手机屏幕大小。


还有人说可以通过一下方法设置自适应

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

我试了,但是没有达到预期的效果

SDK对这两个方法的描述是这样的:

    /**
     * Sets whether the WebView should enable support for the "viewport"
     * HTML meta tag or should use a wide viewport.
     * When the value of the setting is false, the layout width is always set to the
     * width of the WebView control in device-independent (CSS) pixels.
     * When the value is true and the page contains the viewport meta tag, the value
     * of the width specified in the tag is used. If the page does not contain the tag or
     * does not provide a width, then a wide viewport will be used.
     *
     * @param use whether to enable support for the viewport meta tag
     */
    public synchronized void setUseWideViewPort(boolean use) {
        throw new MustOverrideException();
    }


/**
     * Sets whether the WebView loads pages in overview mode, that is,
     * zooms out the content to fit on screen by width. This setting is
     * taken into account when the content width is greater than the width
     * of the WebView control, for example, when {@link #getUseWideViewPort}
     * is enabled. The default is false.
     */
    public void setLoadWithOverviewMode(boolean overview) {
        throw new MustOverrideException();
    }

这里没有深入研究。希望大神指导







0 0
原创粉丝点击