webview里面的片段android在浏览器中打开应用程序外部

时间:2014-01-16 19:20:00

标签: android webview fragment

我在我的应用程序中获得了一个webview,它在它之外打开,而我希望它在内部打开...我已经阅读了stackoverflow上的所有示例,但仍然不知道为什么它就像这样

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文件:

<?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>

5 个答案:

答案 0 :(得分:26)

试试这段代码,你必须覆盖“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;
    }
});

答案 1 :(得分:4)

你需要试试这个

0   "top"
1 0 "middle1"
3 1 "bottom3"

答案 2 :(得分:1)

以下代码为我工作:

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");

答案 3 :(得分:0)

试试这个:

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

答案 4 :(得分:0)

试试这个...当你点击片段中的网页视图时,它不会要求你在新的浏览器应用程序中打开!!

    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 ;
}