android使用webview加载h5页面
之前直接使用webView遇到了一些坑这是最终版记录一下
前台页面java
package com.xiaocaoer.webviewke;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private WebView webview;
private ConstraintLayout constraintLayout;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
linearLayout = findViewById(R.id.linearLayout01);
webview = new WebView(MainActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
webview.setLayoutParams(layoutParams);
// 清缓存和记录,缓存引起的白屏
webview.clearCache(true);
webview.clearHistory();
webview.requestFocus();
WebSettings settings = webview.getSettings();
// loading = findViewById(R.id.loadView);
settings.setJavaScriptEnabled(true);//必须
settings.setCacheMode(WebSettings.LOAD_DEFAULT);//关闭webview中缓存
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);//提高渲染的优先级
settings.setAllowContentAccess(true);//是否允许在WebView中访问内容URL
settings.setBuiltInZoomControls(true);//是否使用其内置的变焦机制
settings.setJavaScriptCanOpenWindowsAutomatically(true);//是否允许自动打开弹窗
settings.setDomStorageEnabled(true);//是否开启DOM存储API权限
settings.setDatabaseEnabled(true);
settings.setUseWideViewPort(true);//WebView是否支持HTML的“viewport”标签或者使用wide viewport。自适应屏幕大小
//解决支持缩放问题;
settings.setLoadWithOverviewMode(true);
// String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath() + "/webcache";
// 设置 Application Caches 缓存目录
// settings.setAppCachePath(appCachePath);
// settings.setDatabasePath(appCachePath);
// 应用可以有缓存 true false 没有缓存
// settings.setAppCacheEnabled(false);
webview.loadUrl("http://192.168.46.184:8089/");
webview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.d("加载", "on page progress changed and progress is" + newProgress);
}
});
webview.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// 加载网页失败时处理 如:
view.loadDataWithBaseURL(null,
"<span>页面加载失败,请确认网络是否连接</span>",
"text/html",
"utf-8",
null);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!webview.getSettings().getLoadsImagesAutomatically()) {
webview.getSettings().setLoadsImagesAutomatically(true);
}
Log.d("加载", "end ");
}
/* @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不另跳浏览器
// 在2.3上面不加这句话,可以加载出页面,在4.0上面必须要加入,不然出现白屏
if (url.startsWith("http://") || url.startsWith("https://")) {
view.loadUrl(url);
webview.stopLoading();
return true;
}
return false;
}*/
});
linearLayout.addView(webview);
}
}
前台页面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"
android:id="@+id/constraintLayoutId"
>
<LinearLayout
android:id="@+id/linearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>