简体   繁体   中英

Android Webview specific content of the page to zoom by default

I am using WebView in my App when URL loaded the first time, it should show the specific content of the page to zoom by default. I have tried with zoom functions but it doesn't work.

To show specific content to the page (in a zoomed-in or zoomed-out state), you can set the scale using setInitialScale()

class WebviewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.webview_activity)
        val myWebView: WebView = findViewById(R.id.my_web_view)
        myWebView.loadUrl("https://www.example.com")

        // use a number greater than 100 to zoom in closer.
        // my emulator maxes out around 470 (after which higher numbers do not make a difference).
        myWebView.setInitialScale((100 * myWebView.scale).toInt())

        myWebView.settings.useWideViewPort = true // support a wide viewport
        myWebView.settings.loadWithOverviewMode = true // fits contents to the screen by width
    }
}

I added two methods from WebSettings : useWideViewPort which allow WebViews to support a wide viewport and loadWithOverviewMode which fits contents onto the screen by width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM