首页 > 解决方案 > WebView 正在剪辑 android 4.4 中的网页

问题描述

WebView 正在剪辑 Android 4.4 上的网页。这是它的外观: 在 Web 视图中

它在谷歌浏览器中正确打开。我希望它在 webview 中像这样打开。 在 Chrome 中

以下是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=findViewById(R.id.web_view);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView .getSettings().setLoadWithOverviewMode(true);
        webView .getSettings().setUseWideViewPort(true);
        webView.loadUrl("https://viftbox.com/");
    }
}

标签: androidwebviewandroid-webview

解决方案


对于在 Android 上使用最新的 WebView 引擎并且您不能使用 Android System WebView 或 Google Chrome:

在 Android 5.0 之前,对 Android WebView 的改进仅限于操作系统升级/更新。所以建议使用NOW DEPRECATED Crosswalk Project作为 WebView。虽然该站点不再存在,但它的幽灵存在于 archive.org我不知道该站点是否有足够的站点存在以发挥作用。如果您无法更改硬件并且需要更新的 webkit 功能,这可能是一个解决方案。

由于 Crosswalk 项目不再更新,或者它没有您需要的功能,我了解到 Mozilla 有一个GeckoView包装了他们的 Gecko 渲染引擎。

请注意,根据 Mozilla 的文档,他们有自己的 API,因此 GeckoView不是Android 的 WebView 的替代品,因此必须为集成做出一些努力。

无论哪种方式,包括自定义 webview 引擎都会增加 APK 大小。


推荐阅读