15

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.

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);
4

8 回答 8

29
getSettings().setBuiltInZoomControls(false);

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

On API >= 11, you can use:

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");
  }
}
于 2012-05-26T13:29:47.043 回答
13

Change this on your AndroidManifest:

    android:minSdkVersion="8"

to this:

    android:minSdkVersion="11"

than use this in your web view settings:

getSettings().setBuiltInZoomControls(true);
    getSettings().setDisplayZoomControls(false);
于 2014-05-01T19:46:14.997 回答
5

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

于 2012-07-09T11:10:38.070 回答
4

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

于 2016-08-30T10:57:06.400 回答
3

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);
于 2012-05-26T13:31:08.697 回答
3

Just add this line:

webSettings.setDisplayZoomControls(false);
于 2015-04-12T20:24:29.380 回答
1

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;

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);
    }

}
于 2016-08-12T13:19:35.007 回答
0

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 multiTouchZoom is true and buttonsZoom is false.

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

于 2012-08-05T11:39:26.987 回答