Is there a way to hide and show the zoom controls

2020-04-05 07:25发布

I have a two Views. One is a WebView and the other is an ImageView. I set the auto zoom controls for the WebView like so:

webview.getSettings().setBuiltInZoomControls(true);

I am just changing the visibility of both Views using GONE and Visible. While changing my View from WebView to ImageView, the zoom controls do not lose their visibility for some period of time.

Is there any possible way to hide and show the Autozoomcontrols?

Edit:

I tried these methods, but it's still not working.

webview.getZoomControls().setVisibility(View.GONE);
webview.getZoomControls().invalidate();

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-05 07:50

You can take the view you get from WebView.getZoomControls() and add it as a child to a layout beside (or on top of) the webview. Unfortunately it's hacky, getZoomControls is deprecated, and you don't get pinch zoom unless you call WebSettings.setBuildInZoomControls(true) (which will add a SECOND zoom controls view, which you don't want), but it seems to work. Here's my code to do this, for what it's worth:

Layout xml:

<RelativeLayout android:id="@+id/rl"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <WebView android:id="@+id/wv"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>

    <FrameLayout android:id="@+id/wv_zoom_fl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" 
        android:layout_alignParentBottom="true" />

</RelativeLayout>

Java (within an activity):

WebView wv = (WebView)findViewById(R.id.wv);
FrameLayout zoomfl = (FrameLayout)findViewById(R.id.wv_zoom_fl);
zoomfl.removeAllViews();
zoomfl.addView(wv.getZoomControls());

Now to have the zoom disappear, you can set the visibility of R.id.rl (which contains the WebView and the FrameLayout containing the zoom conrols) to RelativeLayout.GONE.

For an unhacky solution: if your minimum Api is set to level 11 (which is 3.0 Honeycomb, so unfortunately this is unlikely), you can use: http://developer.android.com/reference/android/webkit/WebSettings.html#setDisplayZoomControls(boolean)

查看更多
甜甜的少女心
3楼-- · 2020-04-05 07:57

I suppose this will work.

webView.getSettings().setUseWideViewPort(true);
查看更多
放我归山
4楼-- · 2020-04-05 08:03
public void setZoomControlGone(View view){
    Class<?> classType;
    Field field;
    try {
        classType = WebView.class;
        field = classType.getDeclaredField("mZoomButtonsController");
        field.setAccessible(true);
        ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view);
        mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
        try {
            field.set(view, mZoomButtonsController);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
}
查看更多
登录 后发表回答