单击 Web 视图中的表单字段时禁用缩放?

发布于 2024-10-14 22:20:26 字数 440 浏览 2 评论 0原文

我已经浏览了几十页类似的问题,但没有一个有任何答案,所以希望这一页会有所不同。

我有一个网络视图,我不希望视图的缩放从我设置的初始缩放级别发生变化。 当前唯一改变缩放级别的是文本框聚焦时

我需要能够通过 Java 代码来完成此操作,而不是使用视口元标记。

只是为了让我没有常见的响应,我在代码中添加了以下内容来禁用缩放和缩放控件:

mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);

我认为可能的解决方案是检查 onFocus 甚至 onClick 事件何时发生在WebView 然后 ZoomOut,但我什至不确定这是否可能?

任何建议将不胜感激。

I've looked through dozens of pages if similar questions, none of them have any answers, so hopefully this one will be different.

I have a webview, and I do not want the zoom of the view to change from the initial zoom level I have it set to. The only thing which changes the zoom level currently is when a text box is focused.

I need to be able to do this through Java code, not using the viewport meta tag.

Just so I don't have the common responses, I have the following in my code to disable zooming, and the zoom controls:

mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);

I'm thinking that a possible solution is to check to see when an onFocus or even an onClick event occurs within the WebView and then zoomOut, but I'm not even sure if that is possible?

Any suggestions would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦里梦着梦中梦 2024-10-21 22:20:26

更新 这个答案是差不多6年前写的,从那时起所有新的android版本都出现了,这很可能已经过时了。

这件事引起了很大的头痛,但最后感谢 setDefaultZoom(ZoomDensity.FAR); 解决了

这一问题,重要的是 onCreateloadUrl 在 WebSettings 之前调用,否则它造成了强制关闭的情况。这里是整个代码,包括导入(对于 Java 新手用户)

package com.my.app;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;

import com.phonegap.*;

public class MyDroidActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        WebSettings settings = appView.getSettings();
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDefaultZoom(ZoomDensity.FAR);
    }
}

UPDATE This answer was written almost 6 years ago, with all the new android versions that came since then, this is most likely outdated.

This thing caused a major headache, but finally was solved thanks to setDefaultZoom(ZoomDensity.FAR);

One thing which is important is that onCreate and loadUrl get called before the WebSettings, otherwise it caused a force close situation. Here the ENTIRE code including imports (for the novice Java users)

package com.my.app;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;

import com.phonegap.*;

public class MyDroidActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        WebSettings settings = appView.getSettings();
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDefaultZoom(ZoomDensity.FAR);
    }
}
童话里做英雄 2024-10-21 22:20:26

我在 HTC 手机上通过添加一个带有 onScaleChanged 空监听器的 WebViewClient 解决了这个问题。我的应用程序是 PhoneGap,所以这就是它的样子,但是添加侦听器在非 PhoneGap 应用程序中应该看起来相同:

public class Main extends DroidGap {

private class NoScaleWebViewClient extends GapViewClient {

    public NoScaleWebViewClient(DroidGap ctx) {
        super(ctx);
    }

    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        Log.d("NoScaleWebViewClient", "Scale changed: " + String.valueOf(oldScale) + " => " + String.valueOf(newScale));
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.init();
    setWebViewClient(appView, new NoScaleWebViewClient(this));
    // disables the actual onscreen controls from showing up
    appView.getSettings().setBuiltInZoomControls(false);
    // disables the ability to zoom
    appView.getSettings().setSupportZoom(false);
    appView.getSettings().setDefaultZoom(ZoomDensity.FAR);
    appView.setInitialScale(100);
}

}

奇怪的是,onScaleChange 侦听器永远不会被调用 - 通过侦听缩放,它会阻止缩放正在发生。我发现我需要所有其他调用(setSupportZoom、setDefaultZoom、setInitialScale)才能使其正常工作,删除它们中的任何一个都会恢复到旧的、有缺陷的行为。

I solved this on HTC phones by adding a WebViewClient with an empty listener for onScaleChanged. My app is PhoneGap, so this is what it looks like, but adding the listener should look the same in a non-PhoneGap app:

public class Main extends DroidGap {

private class NoScaleWebViewClient extends GapViewClient {

    public NoScaleWebViewClient(DroidGap ctx) {
        super(ctx);
    }

    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        Log.d("NoScaleWebViewClient", "Scale changed: " + String.valueOf(oldScale) + " => " + String.valueOf(newScale));
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.init();
    setWebViewClient(appView, new NoScaleWebViewClient(this));
    // disables the actual onscreen controls from showing up
    appView.getSettings().setBuiltInZoomControls(false);
    // disables the ability to zoom
    appView.getSettings().setSupportZoom(false);
    appView.getSettings().setDefaultZoom(ZoomDensity.FAR);
    appView.setInitialScale(100);
}

}

Strangely, the onScaleChange listener never gets called -- by listening for the zoom, it blocks the zoom from happening. I've found that I need all the other calls (setSupportZoom, setDefaultZoom, setInitialScale) in order for this to work, and removing any of them reverts to the old, buggy behavior.

柠檬色的秋千 2024-10-21 22:20:26

我也有同样的麻烦。我需要找到一种方法将 webview 的内容缩放到精确值,一切正常,直到用户开始输入文本。有些方法适用于相对较新的 Android 4.0+ 设备,但在旧设备上失败。在任何地方都有效的唯一方法是设置缩放值,而不是在 Java 中,而是在视口中设置,如下所示

<meta name="viewport" content="initial-scale=.80; maximum-scale=.80; minimum-scale=.80;" />

它适用于我测试的每个设备。

I had the same trouble. I needed to find a way to scale content of webview to exact value, everything worked fine until user starts to input text. There are methods that work on relatively new devices android 4.0+ but fails on old ones. The only way that works everywhere is setting the zoom value not in Java but in viewport like this

<meta name="viewport" content="initial-scale=.80; maximum-scale=.80; minimum-scale=.80;" />

It works on every device I tested.

苦笑流年记忆 2024-10-21 22:20:26

您是否尝试在视口标签中禁用用户可缩放?不确定这是否对你有用,但对我有用。我不需要在java方面做任何事情。

<meta name="viewport" content="user-scalable=no, width=device-width" />

Did you try to disable the user-scalable in the viewport tag? Not sure if that will work for you, but it works for me. I did not need to do anything on the java side.

<meta name="viewport" content="user-scalable=no, width=device-width" />
去了角落 2024-10-21 22:20:26

我也遇到过这个问题,我是这样解决的:

myWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);

在三星Galaxy Tab上运行正常。我希望这会对您有所帮助。

I have encountered this problem too, and I solved it like this:

myWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);

It's runing normally on Sumsung Galaxy Tab. I hope this will help you.

拥抱没勇气 2024-10-21 22:20:26

WebView 有一个特殊的“东西”,我认为它会在这里引发许多问题和答案。发生的情况是,当加载 URL 时,默认的 Android 浏览器会通过 Intent 启动来处理此问题。 缩放在此浏览器中进行,而不是在您的 Web 视图中进行。

解决方案:您需要添加一个 WebviewClient 来告诉 Android 您处理自己浏览一下。示例:

// Use WebView and disable zooming

public class MyWebView extends Activity {
    // nested class
    private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true
        }
    }

    private WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.setInitialScale(500);   // added after user comment

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setSupportZoom(false);

        mWebView.loadUrl("http://www.google.com");      

    }
}

我的 ma​​in.xml 如下所示

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

此代码禁用了运行 Android 2.2 的 HTC Desire 的缩放功能。点击 HTML 输入字段没有什么区别。

WebView/HelloWebViewClient 的整个主题以及正确处理“后退”按钮的重要提示都记录在 你好视图,Web 视图。对于任何使用 WebView 的人来说,这应该是必读的一本书。

The WebView has one special "thing", which I think it will trigger many questions and answers here. What happens is, that when an URL is loaded, the default Android Browser kicks in through an Intent to handle this. The zooming takes part in this browser, not in your Webview.

Solution: You need to add a WebviewClient to tell Android that you handle the browsing yourself. An example:

// Use WebView and disable zooming

public class MyWebView extends Activity {
    // nested class
    private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true
        }
    }

    private WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.setInitialScale(500);   // added after user comment

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setSupportZoom(false);

        mWebView.loadUrl("http://www.google.com");      

    }
}

My main.xml looks like this

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

This code disabled zooming on my HTC Desire running Android 2.2. Tapping into HTML Input fields makes no difference.

The whole topic of WebView/HelloWebViewClient as well as an important hint to handle the "Back" button correctly is documented in Hello Views, Web View. It should be required reading for anybody who uses WebView.

时光倒影 2024-10-21 22:20:26

我相信您可以使用 WebView.setInitialScale 方法设置缩放级别。它需要一个 int 作为比例,所以我猜你会想要做类似 myWebView.setInitialScale(100) 的事情。

I believe you can set the zoom level with WebView.setInitialScale method. It takes an int as scale so I guess you would want to do something like myWebView.setInitialScale(100).

甜妞爱困 2024-10-21 22:20:26

此问题已通过 HTC 设备上的固件更新得到解决,它(显然)是由 Sense UI 错误地覆盖默认 Android 功能引起的。

很难准确地提供有关此问题何时得到纠正的信息,但是当在具有最新固件的任何 HTC 设备上单击文本框时,我的 Web 应用程序不再缩放。

以下两行代码将禁用 android webview 的“缩放”功能:

// disables the actual onscreen controls from showing up
mWebView.getSettings().setBuiltInZoomControls(false);
// disables the ability to zoom
mWebView.getSettings().setSupportZoom(false);

This issue has been fixed by a firmware update on HTC devices, it was (apparently) being caused by the Sense UI overriding default Android functionality incorrectly.

It is very difficult to provide information on exactly when this was corrected, however my web application no longer zooms when a text box is clicked on any HTC device with the latest firmware.

The following two lines of code will disable the "zoom" aspects of an android webview:

// disables the actual onscreen controls from showing up
mWebView.getSettings().setBuiltInZoomControls(false);
// disables the ability to zoom
mWebView.getSettings().setSupportZoom(false);
小霸王臭丫头 2024-10-21 22:20:26

这也让我头疼,但幸运的是我找到了这篇文章:如何在移动设备上停止放大输入焦点

在 css 文件中将输入元素中文本的字体大小设置为 16px(或更大)。

  input {
        font-size: 16px;
}

这是相当黑客,但如果没有其他工作......

This was headache for me too, but fortunately I have found this article: How to stop zoom in on input focus on mobile devices.

Set font size of the text in the input element to 16px (or more) in the css file.

  input {
        font-size: 16px;
}

It is rather hack, but if nothig else works ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文