标签活动(片段)内的WebView

时间:2017-07-21 10:44:04

标签: android android-layout android-fragments android-webview

我正在开发一个简单的webview应用程序,它有三个选项卡(片段)。每个片段都应显示一个webview,但我无法在片段中显示webview。所以请帮助我。

package com.app.examguru;

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;
import android.webkit.WebViewClient;

import com.app.examguru.R;

public class FEED extends Fragment {
WebView webview1; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.feed, container, false);
    WebView webview1 = (WebView) v.findViewById(R.id.webview1);
    webview1.getSettings().setJavaScriptEnabled(true);
    webview1.setWebViewClient(new WebViewClient());
    webview1.loadUrl("http://www.google.com");
    return v;
}
}

1 个答案:

答案 0 :(得分:2)

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class WebFragment extends Fragment {

    private WebView webview;
    private ProgressBar progress;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_web, container, false);

        init(view);

        return view;
    }

    private void init(View view) {

        progress = (ProgressBar) view.findViewById(R.id.progress_bar);
        webview = (WebView) view.findViewById(R.id.webView1);
        WebSettings webSettings = webview.getSettings();
        webSettings.setBuiltInZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webview.loadUrl("http://www.googlecom");

        webview.setWebViewClient(new myWebClient());

    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            progress.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            progress.setVisibility(View.GONE);
        }
    }
}