How to remove zoom buttons on Android webview?

2019-02-02 15:34发布

问题:

Is it possible to remove the zoom buttons ? 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);

回答1:

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


回答2:

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


回答3:

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



回答4:

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


回答5:

Just add this line:

webSettings.setDisplayZoomControls(false);


回答6:

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



回答7:

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

}


回答8:

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.