gpt4 book ai didi

android - 选项卡式 Activity ( fragment )内的 WebView

转载 作者:行者123 更新时间:2023-11-29 19:11:17 25 4
gpt4 key购买 nike

我正在开发一个简单的 webview 应用程序,它具有三个选项卡( fragment )。每个 fragment 都应显示一个 WebView ,但我无法在 fragment 内显示 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;
}
}

最佳答案

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

关于android - 选项卡式 Activity ( fragment )内的 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45235636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com