XML:
<LinearLayout android:orientation="vertical" 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="com.example.mmaster.web_myapplication.Web_Activity"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/m_web"></WebView> </LinearLayout>
Java代码:
package com.example.mmaster.web_myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; /** * A simple {@link Fragment} subclass. */ public class BFragment extends Fragment { private String baidu="https://www.hao123.com/?tn=97638841_hao_pg"; private WebView m_web; public BFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View inflate = inflater.inflate(R.layout.fragment_b, container, false); initView(inflate); return inflate; } private void initView(View inflate) { m_web = (WebView) inflate.findViewById(R.id.m_web); WebSettings settings = m_web.getSettings();//调全局 settings.setJavaScriptEnabled(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); m_web.loadUrl(baidu);//百度网址 m_web.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(baidu); return true; } }); } }