首先fragment布局中放入webview,然后初始化webview
WebView webView;
private WebSettings webSettings;
然后初始化
private void webShow(){
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(“https://www.baidu.com”);
}
接着定义一个MyWebViewClient
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
}
最后在onActivityCreated中调用
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
webShow(); //调用
}