有没有办法在 WebView 上隐藏和显示缩放控件?

发布于 2024-09-07 18:51:38 字数 442 浏览 7 评论 0原文

我有两个观点。一种是WebView,另一种是ImageView。我为 WebView 设置了自动缩放控件,如下所示:

webview.getSettings().setBuiltInZoomControls(true);

我只是使用 GONEVisible 更改两个视图的可见性。将视图从 WebView 更改为 ImageView 时,缩放控件在一段时间内不会失去可见性。

有没有可能隐藏和显示自动缩放控件的方法?

编辑:

我尝试了这些方法,但仍然不起作用。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶…霜飞 2024-09-14 18:51:38
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();
    }
}
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();
    }
}
一场春暖 2024-09-14 18:51:38

您可以获取从 WebView.getZoomControls() 获得的视图,并将其作为子视图添加到 Web 视图旁边(或顶部)的布局中。不幸的是,它很老套, getZoomControls 已被弃用,并且除非您调用 WebSettings.setBuildInZoomControls(true) (这将添加您不想要的第二个缩放控件视图),否则您不会获得捏缩放,但它似乎可以工作。这是我执行此操作的代码,因为它的价值:

布局 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 (在活动内):

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

现在要使缩放消失,您可以设置 R.id.rl 的可见性(其中包含 WebView 和 FrameLayout,其中包含缩放控件)到RelativeLayout.GONE。

对于一个简单的解决方案:如果您的最低 Api 设置为级别 11(即 3.0 Honeycomb,因此不幸的是,这不太可能),您可以使用:
http://developer .android.com/reference/android/webkit/WebSettings.html#setDisplayZoomControls(boolean)

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)

柏林苍穹下 2024-09-14 18:51:38

我想这会起作用。

webView.getSettings().setUseWideViewPort(true);

I suppose this will work.

webView.getSettings().setUseWideViewPort(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文