如何设置初始变焦/宽的web视图 [英] How to set the initial zoom/width for a webview

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

问题描述

我想获得的WebView有类似行为的Andr​​oid浏览器。该浏览器打开一个试图以适应它们的宽度屏幕方式的所有页面。然而,web视图的默认行为是开始于一个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'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.

推荐答案

以下code加载谷歌主页完全缩小以适合 web视图中的桌面版我在Android 2.2的854x480的一个像素的屏幕上。当我重新调整了设备,并重新加载在纵向或横向,页面宽度完全适合视图中的每个时间。

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>

Browser.java:

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();
        }
    }
}

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

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