如何在片段中加载webview中的url链接?

时间:2017-04-17 21:33:12

标签: android android-fragments webview

我想在片段中加载Webview中的url。请参阅我的Manifest和page_1文件。片段中的页面从定义的颜色变为白色,但没有显示像谷歌搜索页面。

page_1.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_page_1, container,false);
String url = "https://www.google.com/";
WebView view = (WebView) v.findViewById(R.id.web1);

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

fragment_page_1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0066ff"
android:orientation="vertical"
tools:context="layout.page_1">

<!-- TODO: Update blank fragment layout -->
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/web1"/>
</LinearLayout>

清单

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

1 个答案:

答案 0 :(得分:2)

我已经尝试过您的代码..并且工作正常..可以尝试清理您的应用缓存

public class FragmentWeb extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_page, container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        String url = "https://www.google.com/";
        WebView web = (WebView) view.findViewById(R.id.web1);

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

        web.loadUrl(url);

    }
}