【问题标题】:Enabling viewport pinch-zoom in Phonegap Android application has no effect在 Phonegap Android 应用程序中启用视口捏合缩放无效
【发布时间】:2012-04-15 11:05:27
【问题描述】:

我需要能够使用 Android PhoneGap 应用上的视口设置打开和关闭捏合缩放。

但在使用 PhoneGap 时,我根本无法让视口影响捏合缩放。 我无法在 PhoneGap 中打开 Pinch-zoom,但使用 Android Native 浏览器浏览网页时一切正常。

我花了很多时间寻找解决方案,但没有任何结果。

我创建了一个非常简单的 index.html 用于测试,具有如下视口设置:

<meta name="viewport" content="user-scalable=yes" />

正如一些人建议的那样,我也尝试了视口:

<meta name="viewport" content="user-scalable=1" />

Android PhoneGap 的其他解决方案是启用此处建议的 Android Native Pinch-Zoom: zoom in phonegap for android 但这对我不起作用,因为我需要能够从 javascript 控制捏缩放。 IE。我需要能够使用 javascript 来更改视口设置,以控制何时可用捏缩放 (user-scalable=yes / user-scalable=no)

请注意“初始比例”和“目标密度dpi”等其他视口设置似乎可以正常工作。例如设置 initial-scale=2 启动应用缩放。

(我目前正在 PhoneGap 1.4.1 和 HTC Desire HD Android 2.3.3 上测试双指缩放)

我开始认为我错过了一些明显的东西,因为我发现关于类似问题的信息太少了......

最小的 index.html:

<!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="user-scalable=yes" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Minimal AppLaud App</title>
      <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
      <script type="text/javascript" charset="utf-8">

        var onDeviceReady = function() {
            document.getElementById("devready").innerHTML = "OnDeviceReady fired.";
        };

        function init() {
            document.addEventListener("deviceready", onDeviceReady, true);
        }   
      </script>  

  </head>
  <body onload="init();" id="stage" class="theme">
    <h2>Minimal AppLaud App</h2>
    <p> LOTS OF TEXT WHICH HAVE BEEN REMOVED HERE </p>

    <p><span id="devready">onDeviceReady not fired.</span></p>

  </body>
</html>

欣赏您可能有的任何想法!

【问题讨论】:

标签: android cordova


【解决方案1】:

您在使用 PhoneGap Build 吗? PhoneGap Build 无法实现。

如果您还没有使用 Desktop PhoneGap,请按照 PhoneGap Getting Started with Android Guide 并在 Eclipse 项目中设置您的代码。

设置完成后,编辑您的 Main Java 文件,如入门指南中所述: 添加以下导入:

import android.webkit.WebSettings; 
import android.webkit.WebSettings.ZoomDensity;

在'super.loadUrl("file:///android_asset/www/index.html");'下面您输入的行,作为启动指南的一部分,添加:

settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setDefaultZoom(ZoomDensity.MEDIUM); 

清理并构建您的项目 - 现在应该允许缩放。

要限制用户可以放大多远,请添加一个元标记,如下面的每个 HTML 页面:

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, minimum-scale=1, maximum-scale=1.5, user-scalable=1">

【讨论】:

  • 就我而言,我不得不使用这个:super.appView.getSettings().setBuiltInZoomControls(true); super.appView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM); super.appView.getSettings().setSupportZoom(true);
  • 如果您在执行此操作时遇到构建错误,请尝试以下@jop​​in 的答案。
【解决方案2】:

刚刚确认 Pinch Zoom 适用于以下 Android 手机:

  • 三星 Galaxy S820L Android 4.4

  • LG Sunset L33L Android 5.0

在 Windows 上使用 PhoneGap CLI(不是服务):

C:\>phonegap --version
5.4.1

Android SDK 工具版本:

24.4.1 (from the SDK Manager)

这是 MainActivity.java 的代码(platforms\android\src\com\phonegap\helloworld):

package com.phonegap.helloworld;

import android.os.Bundle;
import org.apache.cordova.*;

import android.webkit.WebView;
import android.webkit.WebSettings;

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);

        WebView webView = (WebView) appView.getEngine().getView();
        WebSettings settings = webView.getSettings();

        settings.setBuiltInZoomControls(true);
        settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        settings.setSupportZoom(true);
    }
}

这是在 index.html 中:

 <meta name="viewport" content="user-scalable=yes, initial-scale=1, maximum-scale=3, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

一个额外的注意事项 - 与此通话:

settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

在构建时得到了这个:

uses or overrides a deprecated API - Note: Recompile with -Xlint:deprecation for details

注释掉了,Zoom 不受影响。

谢谢大家!!!

【讨论】:

  • 谢谢,这很有帮助!
  • 太棒了!!太感谢了!!非常适合我的 Kindle Fire 7(第 7 代)应用程序,该应用程序具有元标记,可以使某些页面可缩放,而有些则不能。
【解决方案3】:

即使在这之后没有找到解决方案的人,我在 Github 中找到了答案:

https://github.com/LouisT/cordova-useragent/issues/1

基本上你必须:

import android.webkit.WebView;

然后:

// super.appView.getSettings().setBuiltInZoomControls(true);
// super.appView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM); 
// super.appView.getSettings().setSupportZoom(true);
// Instead use this
WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setDefaultZoom(ZoomDensity.MEDIUM);
settings.setSupportZoom(true);

【讨论】:

  • 我将从其他一些线程链接到这个答案,因为很多人都提到了 webview 对象而没有显示它应该如何初始化。
【解决方案4】:

对于cordova 5.x.x,您需要使用https://github.com/LouisT/cordova-useragent/issues/1的解决方案

import android.webkit.WebView;

WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setDefaultZoom(ZoomDensity.MEDIUM);
settings.setSupportZoom(true);

对我来说,需要将 ZoomDensity.MEDIUM 更改为 WebSettings.ZoomDensity.MEDIUM

【讨论】:

    【解决方案5】:

    我有完全相同的问题,除了测试不同的视口设置外,我还尝试了以下建议:Pinch Zoom in Android with Phonegap and jQUERY Mobile 但我无法使其工作(在三星 Galaxy SII、Android 4.0.3 上测试)。但是,其他人报告使用链接中的提示成功进行了捏缩放。希望对你也有帮助!

    【讨论】:

      【解决方案6】:

      html5 文档应以:

      <!DOCTYPE html>
      

      如果没有这一行,Phonegap Android 中的视口 AppBrowser 将无法工作

      【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2016-11-25
      • 1970-01-01
      • 2018-06-22
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2011-10-31
      相关资源
      最近更新 更多