如何在android中缩放web视图页面

时间:2016-02-26 12:54:59

标签: android webview zoom

大家好我在android中使用WebView我希望缩放在我的网页视图中加载的页面。我使用以下设置:

    wb.getSetting().setBuiltInZoomControls(true);
    wb.getSetting().setSupportZoom(true);
    wb.loadUrl("https://www.mywebsite.com/");

在网页加载缩放控件之前显示正确,但在网页加载后,缩放控件就消失了。

Screen shot

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个:

public class WebViewController extends WebView {

    private ZoomButtonsController zoom_controll = null;

    public WebViewController(Context context) {
        super(context);
        disableControls(); 
    } 

    public WebViewController(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        disableControls(); 
    } 

    public WebViewController(Context context, AttributeSet attrs) {
        super(context, attrs);
        disableControls(); 
    } 

    /** 
     * Disable the controls 
     */ 
    private void disableControls(){ 
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            // Use the API 11+ calls to disable the controls 
            this.getSettings().setBuiltInZoomControls(true);
            this.getSettings().setDisplayZoomControls(false);
        } else { 
            // Use the reflection magic to make it work on earlier APIs 
            getControlls(); 
        } 
    } 

    /** 
     * This is where the magic happens :D 
     */ 
    private void getControlls() { 
        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 ev) {
        super.onTouchEvent(ev);
        if (zoom_controll != null){
            // Hide the controlls AFTER they where made visible by the default implementation. 
            zoom_controll.setVisible(false);
        } 
        return true; 
    } 
}