webview inside fragment android opening outside of

2020-03-14 07:20发布

问题:

I got a webview in my app and it opens outside of it, while I want it to open inside... I've read all examples on stackoverflow and still don't know why it's like this

NewsFragment.java

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.WebView;

public class NewsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.news, container, false);

        WebView webView = (WebView)rootView.findViewById(R.id.webView);

        webView.setInitialScale(1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);

        webView.loadUrl("https://www.google.com");

        return rootView;
    }
}

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

回答1:

Try this code, you have to override the "shouldOverrideUrlLoading()"

String url = "http://appprice.appday.de";

WebView wv = new WebView(context); 
// or 
// WebView wv = (WebView)findViewById(R.id.my_webview);

wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});


回答2:

You need to try this

webview.setWebViewClient(new WebViewClient());


回答3:

Following code work for me:

webView = finder.find(R.id.webView_for_info);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

webView.setWebViewClient(new WebViewClient());
//webView.setWebChromeClient(new WebChromeClient());

webView.loadUrl("file:///android_asset/info.html");


回答4:

try this:

public class WebViewGameActivity extends BaseActivity {
    private static final String TAG = WebViewGameActivity.class.getSimpleName();


    private WebView myWebView;

       @SuppressLint("SetJavaScriptEnabled")
    @Override        
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_webview);


          myWebView = (WebView) findViewById(R.id.webview);

          // Configure related browser settings
          WebSettings webSettings = myWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setLoadsImagesAutomatically(true);

          myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

          // Configure the client to use when opening URLs
          myWebView.setWebViewClient(new MyBrowser());

          // Load the initial URL
          //myWebView.loadUrl("http://www.example.com");

          String path = Uri.parse("file:///android_asset/index.html").toString();
          myWebView.loadUrl(path);


       }//end onCreate



       // Manages the behavior when URLs are loaded
       private class MyBrowser extends WebViewClient {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
             view.loadUrl(url);
             return true;
          }
       }




}//end WebViewGameActivity


回答5:

try this ...as when u click on web view in the fragment it won't ask you to open in a new browser application!!

    String url = "https://www.google.co.in/";
    WebView view = (WebView) rootView.findViewById(R.id.webView1);


    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setLoadWithOverviewMode(true);
    view.getSettings().setUseWideViewPort(true);
    view.getSettings().setBuiltInZoomControls(true);
    view.getSettings().setPluginState(WebSettings.PluginState.ON);
    view.setWebViewClient(new WebViewClient());

    view.loadUrl(url);

    return rootView ;
}