【问题标题】:How to remove zoom buttons on Android webview?如何删除 Android webview 上的缩放按钮?
【发布时间】:2012-06-01 18:13:40
【问题描述】:

是否可以删除缩放按钮?它在我缩放 WebView 时显示。我需要没有这些按钮的缩放控制。我使用的是安卓 2.3。

我使用下面的代码,

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

【问题讨论】:

标签: android zooming android-webview


【解决方案1】:

如果您只想禁用缩放控件,请使用: webView.getSettings().setDisplayZoomControls(false);

【讨论】:

    【解决方案2】:

    可以通过实现您自己的自定义 webview 来删除缩放按钮,并使用反射来获取内置的缩放控件并使它们对于 11 以下的 API(Honeycomb)不可见。因此,该代码适用于所有 API,直到 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);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      只需添加这一行:

      webSettings.setDisplayZoomControls(false);
      

      【讨论】:

      • 那家伙问Android 2.3,这在2.3中是不可能的
      【解决方案4】:

      在您的 AndroidManifest 上更改此项:

          android:minSdkVersion="8"
      

      到这里:

          android:minSdkVersion="11"
      

      比在您的网络视图设置中使用它:

      getSettings().setBuiltInZoomControls(true);
          getSettings().setDisplayZoomControls(false);
      

      【讨论】:

        【解决方案5】:

        这里有一个解决方案:

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

        在您的情况下,multiTouchZoom 为真,buttonsZoom 为假。

        我在这里 enable/disable zoom in Android WebView 找到了它,它对我有用。

        【讨论】:

        • 什么是multiTouchZoom和buttonsZoom变量?
        • @jayellos 用您的布尔值替换它们。如果您想要多点触控缩放,请将 multiTouchZoom 替换为 true。如果需要显示缩放按钮,请将 buttonsZoom 替换为 true。否则,替换为 false。
        【解决方案6】:

        最后我的回答是,在 Android 2.3 中无法隐藏/删除 WebView 上的这些按钮。

        【讨论】:

          【解决方案7】:

          遵循此代码。它会帮助你。

          Main.java

              WebView wv = (WebView) findViewById(R.id.webview1) ;
              WebSettings webSettings = wv.getSettings();
              webSettings.setBuiltInZoomControls(false);
              wv.loadUrl(url);
          

          【讨论】:

          • 充其量,这将启用缩放控件。
          • 将布尔值更改为 false
          【解决方案8】:
          getSettings().setBuiltInZoomControls(false);
          

          使用上面的代码行来移除缩放按钮。

          在 API >= 11 上,您可以使用:

          webView.getSettings().setBuiltInZoomControls(true);
          webView.getSettings().setDisplayZoomControls(false);
          

          更新::: 如果您想在没有缩放控件的情况下进行放大/缩小,请使用以下代码(复制自 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");
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-01
            • 2012-10-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多