【问题标题】:Android WebView bug with zoom带有缩放的 Android WebView 错误
【发布时间】:2019-07-24 22:11:12
【问题描述】:

缩放后 WebView 似乎无法正常工作(通过触摸执行)。 在页面加载缩放页面后重现,但不允许在缩放后滚动视图(这将导致问题不出现)。点击链接。链接突出显示,就像单击它一样,但 WebView 没有导航到下一页(因此在视觉上它保持在相同的 url 上)。 这里是测试项目sourceapk 来重现这个。

在 Nexus S 和 HTC wildfire S 上转载。 如果有任何想法或指示,我将不胜感激。

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class TestWebViewBugActivity extends Activity {

    WebView webView;

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

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);

        webView.setWebViewClient(new WebViewClient() {
        });
        webView.loadUrl("http://google.com/");

    }
}

附: 根据 cmets 的建议更改源和 apk 中的布局 XML(高度现在是静态值)

【问题讨论】:

    标签: android webview


    【解决方案1】:

    我相信您需要修复您的 Main.xml 文件。 WebView 中的布局高度是“fill_parent”。当客户端放大时,WebView 会放大影响父级。这是你的 Main.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"
    >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
        <WebView
            android:id="@+id/webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
        />
    </LinearLayout>
    

    尝试给 WebView 一个定义的高度。然后尝试缩放旅馆,看看会发生什么。

    <?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"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
        <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="500px"
        />
    </LinearLayout>
    

    【讨论】:

    • 不幸的是,这没有帮助。
    【解决方案2】:

    在布局main.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/web_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0" />
    </LinearLayout>
    

    活动中

    WebView webView = (WebView) findViewById(R.id.web_view);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    
    webView.loadUrl("https://google.com"); 
    

    它应该可以工作。 而不是 google.com 尝试其他页面。在我的手机上打开默认浏览器。但是我的私人页面工作正常:)

    【讨论】:

    • 感谢您的检查。默认浏览器打开,因为您删除了 webView.setWebViewClient(new WebViewClient() {});在默认浏览器中没有这样的问题。仅在网络视图中。玩 layout_xxxx 也无济于事。
    • THx 的提示。它现在正常工作。还有一件事,你做了什么 api ver 应用程序?我创建了 for 8 (2.2),它在我的 Galaxy S 和 2.3.4 上运行良好。也许这就是问题所在?
    • 设置为 4 (1.6)。切换到 2.3 并没有帮助。除非您知道如何重现它,否则这不是一件很容易重现的事情(主要是在缩放后不允许视图移动)所以也许您只是没有重现它。
    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2018-01-16
    相关资源
    最近更新 更多