Android 为 Webview 设置缩放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2505393/
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 06:06:02  来源:igfitidea点击:

Set zoom for Webview

android

提问by Farha Ansari

I have a WebView and 2 urls to open it it. What I want to do is, when i set a zoom level for 1st url, and then i go to 2nd url, it should also have the same zoom level. Right now, the zoom level resets for both.

我有一个 WebView 和 2 个 url 来打开它。我想要做的是,当我为第一个 url 设置缩放级别,然后转到第二个 url 时,它也应该具有相同的缩放级别。现在,两者的缩放级别都会重置。

Thanks, Farha

谢谢,法哈

回答by Samuel

this will be applicable in this scenario i believe

我相信这将适用于这种情况

mWebView.setInitialScale(ZOOM_LEVEL);

where ZOOM_LEVEL for example can be 25 for 25% 150 for 150%

例如,其中 ZOOM_LEVEL 可以是 25 表示 25% 150 表示 150%

回答by Praveen

use the webSettingsclass

使用webSettings

webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

Notice that although webSettingsis available since API Level 1, WebSettings.ZoomDensityis available since API Level 7. Works well for all device resolutions.

请注意,虽然webSettings可用,因为API等级1,WebSettings.ZoomDensity是可用,因为API级别7.非常适用于所有的设备分辨率。

Also, in order to enable zoom on the webView, add the following code:

另外,为了在 webView 上启用缩放,添加以下代码:

webView.getSettings().setBuiltInZoomControls(true);

回答by King of Masses

setDefaultZoom is deprecated.

setDefaultZoom 已弃用。

webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

So instead of that you can use like below,

所以你可以像下面这样使用,

webview.setInitialScale(1);
webview.getSettings().setLoadWithOverviewMode(true); 
webview.getSettings().setUseWideViewPort(true);

This help you to remove

这可以帮助您删除

setDefaultZoom(WebSettings.ZoomDensity.FAR).

回答by John Weidner

Before you leave the first page, use

在离开第一页之前,使用

int scale = 100 * webView.getScale();

Then after you load the second page, use

然后在加载第二页后,使用

webView.setInitialScale( scale );

回答by yuttadhammo

@John gave the right idea, but one command is enough, since you can get and set before the page shows:

@John 给出了正确的想法,但是一个命令就足够了,因为您可以在页面显示之前获取和设置:

private class MyWebViewClient extends WebViewClient {
    public void onPageFinished(WebView view, String url) {
        view.setInitialScale((int)(100*view.getScale()));
    }
}

then just set this as your WebView's client:

然后将其设置为您的 WebView 的客户端:

webview.setWebViewClient(new MyWebViewClient());

回答by Sumit Sahoo

You can set the zoom to 0.1f

您可以将缩放设置为 0.1f

mWebView.zoomBy(0.1f);

mWebView.zoomBy(0.1f);

回答by KHALID

try this thing

试试这个

int default_zoom_level=100;
Point Scroll=new Point(0,0);
webview1.setInitialScale(default_zoom_level);
    webview1.loadData("url");

After doing zoomIn/ZoomOut or Scrolling.Since Scale may be get to change so calculate scale level and scrolling along X and Y Axis.

在执行 zoomIn/ZoomOut 或 Scrolling.Since Scale 之后,可能会发生变化,因此计算缩放级别并沿 X 和 Y 轴滚动。

int current_scale_level=(int)webview1.getScale()*100;
Scroll.x=webview1.getScrollX();
Scroll.y=webview1.getScrollY();

then before loading of next webview do this

webview2.setInitialScale(current_scale_level);
    webview2.loadData("url");
webview2.scrollTo(Scroll.x,Scroll.y);

回答by DeepakPanwar

I found this blog helpful:

我发现这个博客很有帮助:

It will help you set initial zoom level and on seek-change content of webview resizes

它将帮助您设置初始缩放级别和 webview 调整大小的搜索更改内容

// Set the initial progress of seek bar mSeekBar.setProgress(mWebView.getSettings().getTextZoom()/25);

// 设置搜索栏的初始进度 mSeekBar.setProgress(mWebView.getSettings().getTextZoom()/25);

    // Set a change listener for seek bar
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            /*
                public abstract void setTextZoom (int textZoom)
                    Sets the text zoom of the page in percent. The default is 100.

                Parameters
                    textZoom : the text zoom in percent
            */

            // Zoom the web page text
            // We will allow text zooming 25% to 300%
            mWebView.getSettings().setTextZoom(i*25);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

http://android--code.blogspot.in/2016/01/android-how-to-change-webview-text-size.html

http://android--code.blogspot.in/2016/01/android-how-to-change-webview-text-size.html

回答by sunil

Zooming of WebView can be controlled programatically by zoomIn()and zoomOut()methods

可以通过zoomIn()zoomOut()方法以编程方式控制 WebView 的缩放