如何设置 webview 的初始缩放/宽度 [英] How to set the initial zoom/width for a webview

查看:36
本文介绍了如何设置 webview 的初始缩放/宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 WebView 具有与 android 浏览器类似的行为.浏览器以尝试使页面宽度适合屏幕的方式打开所有页面.但是,WebView 的默认行为是以 100% 像素比例开始,因此它从左上角开始放大.

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, the default behavior of the WebView is to start at a 100% pixel scale so it starts zoomed in on the top left corner.

我花了几个小时试图找到一种方法让 WebView 像在浏览器中那样将页面缩放到屏幕,但我没有任何运气.有没有人找到一种方法来实现这一点?

I have spent the last couple hours trying to find a way to get the WebView to scale the page to the screen like it does in the browser but I'm not having any luck. Has anyone found a way to accomplish this?

我看到一个名为 setLoadWithOverviewMode 的设置,但它似乎根本没有做任何事情.我也尝试过 setInitialScale 但在不同的屏幕尺寸和网页尺寸上不会像浏览器缩放那样优雅.

I see is a setting called setLoadWithOverviewMode, but that didn't appear to do anything at all. I also experimented with setInitialScale but on different screen sizes and web page sizes that won't be as graceful as the browsers scaling.

有人有线索吗?

谢谢

Brian 的方法似乎在手机处于横向而非纵向时有效.在纵向它很接近但仍然不适合页面中的整个屏幕.我开始这个赏金计划是希望有一种可靠的方法可以使页面在任何方向或屏幕尺寸下都能适应页面的宽度.

Brian's method seems to work when the phone in landscape but not in portrait. In portrait it is close but still does not fit the whole screen in the page. I starting this bounty with the hope there is a sure-fire way to get the initial zoom to fit the page to the width of the page in any orientation or screen size.

推荐答案

以下代码加载桌面版本的 Google 主页完全缩小以适合我在 Android 2.2 上的 webview854x480 像素屏幕.当我重新定向设备并以纵向或横向重新加载时,页面宽度每次都完全适合视图.

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:

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>

浏览器.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();
        }
    }
}

这篇关于如何设置 webview 的初始缩放/宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆