在WebView Android中启用缩放选项

Man*_*anu 5 android webview cordova

我正在使用PhoneGap开发应用程序.我无法在webview中启用内置放大/缩小功能.

我在onCreate函数中使用了以下代码

WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setBuiltInZoomControls(true);
Run Code Online (Sandbox Code Playgroud)

但它没有用.

而Activity类是

activity_main.xml中

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/> 
Run Code Online (Sandbox Code Playgroud)

Swe*_*r ツ 19

检查您是否没有包装Webview 的ScrollView.

似乎ScrollView妨碍了捏手势.

要修复它,只需将您的Webview放在ScrollView外面,然后使用相同的行:

webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
Run Code Online (Sandbox Code Playgroud)

  • 为我工作.对于那些想要捏缩放但没有显示缩放图标的人来说也是一个提示:webSettings.setDisplayZoomControls(false); (4认同)

kri*_*ris 5

对于科尔多瓦 5

Cordova 5.1 的情况略有变化(我认为它实际上在 5.0 中发生了变化)。
要为 Cordova 5 启用 Android 缩放,请添加以下行:

import android.webkit.WebView;
import android.webkit.WebSettings; 
import android.webkit.WebSettings.ZoomDensity;
Run Code Online (Sandbox Code Playgroud)

还有这些

        WebView webView = (WebView) appView.getEngine().getView();
        WebSettings settings = webView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
Run Code Online (Sandbox Code Playgroud)

您的src/com/YOURPACKAGE.java文件的完整示例:

package com.YOURPACKAGE;

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

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


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.setSupportZoom(true);
        //settings.setDefaultZoom(ZoomDensity.FAR);
    }
}
Run Code Online (Sandbox Code Playgroud)