WebView正在裁剪Android 4.4上的网页。外观如下:
它在谷歌浏览器中正确打开。我希望它在webview中这样打开。
以下是我的代码:
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/");
}
}
对于在Android上使用最新的WebView引擎,您不能使用Android System WebView或Google Chrome:
在Android 5.0之前,对Android WebView的改进仅限于操作系统升级/更新。因此,建议使用NOW DEPRECATED Crosswalk Project作为WebView。虽然该站点不再存在,但是its ghost exists on archive.org我不知道是否有足够的站点可以生存下来而有用。如果您无法更改硬件并且需要更多最新的Webkit功能,这可能是一个解决方案。
由于Crosswalk Project不再更新,或者它不具有您需要的功能,因此我了解到Mozilla的GeckoView封装了他们的Gecko渲染引擎。
请注意,根据Mozilla的文档,它们具有自己的API,因此NOT是Android WebView的直接替代品,因此必须付出一些努力进行集成。
无论哪种方式,都会增加APK的大小。