2012-05-26 65 views
10

是否可以卸下變焦按鈕enter image description here?當我放大WebView時顯示它。我需要沒有這些按鈕的變焦控制。我正在使用android 2.3。如何刪除Android webview上的縮放按鈕?

我用下面的代碼,

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

可我們在這裏http://chat.stackoverflow.com/rooms/10629/agarwal –

+0

對沒錯討論。我會參加。 – bharath

+0

@bharath可以幫助我嗎[這](http://stackoverflow.com/questions/11777116/enable-zoom-for-all-pages-of-an-android-application) –

回答

5

最後我的答案是,它無法隱藏/在Android 2.3的去除WebView這些按鈕的。

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

請按照此代碼。它會幫助你。

Main.java

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

充其量,這將*啓用*縮放控件。 – CommonsWare

+0

將布爾值更改爲false –

0

這裏是一個解決方案:

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它對我很有用。

+0

什麼是multiTouchZoom和buttonsZoom變量? – jayellos

+0

@jayellos用你的布爾值替換它們。如果您想要多點觸控縮放,請將'multiTouchZoom'替換爲true。如果您需要顯示縮放按鈕,請將'buttonsZoom'替換爲true。否則,請用false替換。 –

+0

這個解決方案將工作http://stackoverflow.com/a/11901948/998732 – jayellos

7

更改此對您AndroidManifest:

android:minSdkVersion="8" 

這樣:

android:minSdkVersion="11" 

比在Web視圖設置使用此:

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

只需添加這行:

webSettings.setDisplayZoomControls(false); 
+0

這傢伙詢問關於Android 2.3,這是不可能的2.3 – VSG24

1

縮放按鈕可以通過實現您自己的自定義webview,並使用反射來獲取內置的縮放控件,並使它們在11(Honeycomb)以下的API中不可見。該代碼因此適用於所有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); 
    } 

} 
1

如果您希望禁用只縮放控件使用: webView.getSettings().setDisplayZoomControls(false);