如何删除Android webview上的缩放按钮?

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

How to remove zoom buttons on Android webview?

androidzoomandroid-webview

提问by bharath

Is it possible to remove the zoom buttons enter image description here? Its showing when I zooming the WebView. I need zoom control without these buttons. I'm using android 2.3.

是否可以删除缩放按钮在此处输入图片说明?它在我缩放 WebView 时显示。我需要没有这些按钮的缩放控制。我正在使用安卓 2.3。

I used below code,

我使用了下面的代码,

WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setJavaScriptEnabled(true);
FrameLayout mContentView = (FrameLayout) getWindow().
        getDecorView().findViewById(android.R.id.content);
final View zoom = webview.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);

采纳答案by bharath

Finally my answer is, Its not possible to hide/remove these button's on WebViewin Android 2.3.

最后我的答案是,WebView在 Android 2.3 中无法隐藏/删除这些按钮。

回答by Shankar Agarwal

getSettings().setBuiltInZoomControls(false);

use the above line of code to remove the zoom buttons.

使用上面的代码行删除缩放按钮。

On API >= 11, you can use:

在 API >= 11 上,您可以使用:

webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

Updated:::If you want have zoom in/out without zoom controls then use the below code(copied from here)

更新:::如果您想在没有缩放控件的情况下放大/缩小,请使用以下代码(从此处复制)

public class Main extends Activity {
? private WebView myWebView;
? private static final FrameLayout.LayoutParams ZOOM_PARAMS =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM);

? @Override
? protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.webview);
    myWebView = (WebView)findViewById(R.id.webView);

? ? FrameLayout mContentView = (FrameLayout) getWindow().
? ? getDecorView().findViewById(android.R.id.content);
? ? final View zoom = myWebView.getZoomControls();
? ? mContentView.addView(zoom, ZOOM_PARAMS);
? ? zoom.setVisibility(View.GONE);

? ? myWebView.loadUrl("http://www.almondmendoza.com");
? }
}

回答by zvzej

Change this on your AndroidManifest:

在您的 AndroidManifest 上更改此设置:

    android:minSdkVersion="8"

to this:

对此:

    android:minSdkVersion="11"

than use this in your web view settings:

而不是在您的网络视图设置中使用它:

getSettings().setBuiltInZoomControls(true);
    getSettings().setDisplayZoomControls(false);

回答by Piotr Badura

If you want to disable only zoom controls use this: webView.getSettings().setDisplayZoomControls(false);

如果您只想禁用缩放控件,请使用以下命令: webView.getSettings().setDisplayZoomControls(false);

回答by TheOnlyAnil

Just add this line:

只需添加这一行:

webSettings.setDisplayZoomControls(false);

回答by code_finder

Follow this code. It will help you.

遵循此代码。它会帮助你。

Main.java

主程序

    WebView wv = (WebView) findViewById(R.id.webview1) ;
    WebSettings webSettings = wv.getSettings();
    webSettings.setBuiltInZoomControls(false);
    wv.loadUrl(url);

回答by Ovokerie Ogbeta

zoom buttons can be removed by implementing your own custom webview, and using reflection to get the built in zoom controls and making them invisible for API below 11 (Honeycomb). The code is as thus works for all APIs upto android nougat;

缩放按钮可以通过实现你自己的自定义 webview 来移除,并使用反射来获取内置的缩放控件并使它们对于低于 11 (Honeycomb) 的 API 不可见。代码因此适用于所有 API,直到 android nougat;

public class CustomWebView extends WebView{

    private ZoomButtonsController zoom_controll = null;

    public CustomWebView(Context context) {
        this(context, null);
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.webViewStyle);
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initWebSettings();
    }

    @SuppressLint("NewApi")
    private void initWebSettings() {
        WebSettings webSettings = getSettings();

        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            webSettings.setAllowContentAccess(true);
            webSettings.setDisplayZoomControls(false);

        } else {
            try {
                Class webview = Class.forName("android.webkit.WebView");
                Method method = webview.getMethod("getZoomButtonsController");
                zoom_controll = (ZoomButtonsController) method.invoke(this, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(zoom_controll != null)
            zoom_controll.getZoomControls().setVisibility(View.GONE);
        return super.onTouchEvent(event);
    }

}

回答by Dmitry Zaytsev

Here is a solution:

这是一个解决方案:

    if (ev.getAction() == MotionEvent.ACTION_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_1_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_2_DOWN ||
        ev.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
    if (multiTouchZoom && !buttonsZoom) {
        if (ev.getPointerCount() > 1) {
            getSettings().setBuiltInZoomControls(true);
            getSettings().setSupportZoom(true);
        } else {
            getSettings().setBuiltInZoomControls(false);
            getSettings().setSupportZoom(false);
        }
    }
}

if (!multiTouchZoom && buttonsZoom) {
    if (getPointerCount(ev) > 1) {
        return true;
    }
}

In your case multiTouchZoomis true and buttonsZoomis false.

在你的情况下multiTouchZoom是真的,buttonsZoom是假的。

I found it here enable/disable zoom in Android WebViewand it's worked for me.

我在这里发现它在 Android WebView 中启用/禁用缩放,它对我有用