縮放按鈕可以通過實現您自己的自定義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);
}
}
可我們在這裏http://chat.stackoverflow.com/rooms/10629/agarwal –
對沒錯討論。我會參加。 – bharath
@bharath可以幫助我嗎[這](http://stackoverflow.com/questions/11777116/enable-zoom-for-all-pages-of-an-android-application) –