【问题标题】:Is there a way to hide and show the zoom controls on a WebView?有没有办法隐藏和显示 WebView 上的缩放控件?
【发布时间】:2010-07-02 09:38:04
【问题描述】:

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

webview.getSettings().setBuiltInZoomControls(true);

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

有没有办法隐藏和显示自动缩放控件?

编辑:

这些方法我都试过了,还是不行。

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

【问题讨论】:

    标签: android webview zooming


    【解决方案1】:
    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以获取从 WebView.getZoomControls() 获得的视图,并将其作为子项添加到 Web 视图旁边(或顶部)的布局中。不幸的是,它很hacky,不推荐使用 getZoomControls ,除非您调用 WebSettings.setBuildInZoomControls(true) (这将添加您不想要的 SECOND 缩放控件视图),否则您不会得到捏缩放,但它似乎有效。这是我的代码,值得这样做:

      布局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)

      【讨论】:

        【解决方案3】:

        我想这会奏效。

        webView.getSettings().setUseWideViewPort(true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-18
          • 2011-10-13
          • 1970-01-01
          • 2019-08-16
          • 2016-07-04
          相关资源
          最近更新 更多