要支援縮放,肯定要先支援JavaScript,加如下程式碼:
//支援JS
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);123123
重點來了,要想支援縮放,要加如下程式碼支援
//支援螢幕縮放
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
我們怎樣去掉它呢,在Android3.0之後實現非常簡單,加一句程式碼就可以了,如下程式碼:
settings.setDisplayZoomControls(false);
所以說,我們沒有必要相容2.x了,就像iOS開發只相容到IOS7版本就OK了,但是有的同學又說,我有強迫症,我就想相容到2.x版本,其實解決方案網上都有了,這裡用到了Java反射的知識,透過反射來獲取私有的屬性控制元件mZoomButtonsController,然後就其setVisibility(View.GONE)隱藏就可以了,下面貼出程式碼實現:
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);
field.set(view, mZoomButtonsController);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
}
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
要支援縮放,肯定要先支援JavaScript,加如下程式碼:
//支援JS
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);123123
重點來了,要想支援縮放,要加如下程式碼支援
//支援螢幕縮放
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
我們怎樣去掉它呢,在Android3.0之後實現非常簡單,加一句程式碼就可以了,如下程式碼:
settings.setDisplayZoomControls(false);
所以說,我們沒有必要相容2.x了,就像iOS開發只相容到IOS7版本就OK了,但是有的同學又說,我有強迫症,我就想相容到2.x版本,其實解決方案網上都有了,這裡用到了Java反射的知識,透過反射來獲取私有的屬性控制元件mZoomButtonsController,然後就其setVisibility(View.GONE)隱藏就可以了,下面貼出程式碼實現:
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();
}