How to set the initial zoom/width for a webview

后端 未结 8 1367
無奈伤痛
無奈伤痛 2020-11-27 11:08

I am trying to get the WebView to have similar behavior as the android browser. The browser opens all pages in a way that tries to fit their width to the screen. However,

相关标签:
8条回答
  • 2020-11-27 12:05

    The following code loads the desktop version of the Google homepage fully zoomed out to fit within the webview for me in Android 2.2 on an 854x480 pixel screen. When I reorient the device and it reloads in portrait or landscape, the page width fits entirely within the view each time.

    BrowserLayout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
         <WebView android:id="@+id/webview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" />
    </LinearLayout>
    

    Browser.java:

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    
    public class Browser extends Activity {
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.BrowserLayout);
    
            String loadUrl = "http://www.google.com/webhp?hl=en&output=html";
    
            // initialize the browser object
            WebView browser = (WebView) findViewById(R.id.webview);
    
            browser.getSettings().setLoadWithOverviewMode(true);
            browser.getSettings().setUseWideViewPort(true);
    
            try {
                // load the url
                browser.loadUrl(loadUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:10

    Try using a wide viewport:

     webview.getSettings().setUseWideViewPort(true);
    
    0 讨论(0)
提交回复
热议问题