如何在Fragment类中加载WebView?如何在android中加载Webview?[英] How to Load WebView in Fragment class? How to load Webview in android?

本文是小编为大家收集整理的关于如何在Fragment类中加载WebView?如何在android中加载Webview?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试在应加载的片段中创建一个WebView.我已经将其添加到我的XML文件中.但是,FindViewByID方法仅在我扩展活动类时才有效.无论如何,我也可以在片段中使用它吗?

我已经从另一个问题中测试了解决方案,但它不起作用.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    PlanWebView WebView = (PlanWebView) getView().findViewById(R.id.PlanWebView);
    // Unknown class: "PlanWebView";
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

}

推荐答案

请尝试一下,这是 OneFragment 类,WebView需要一个必须使用XML

夸大的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout. fragment_one, container, false);
    mWebView = (WebView) view.findViewById(R.id.webview);
    mWebView.loadUrl("https://google.com");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    return view;
}

和 xml Fragment_one

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

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

其他推荐答案

在片段中您可以在onCreateview方法中声明您的变量.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
// Required empty public constructor
}

@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    }

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


 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
PlanWebView  = (WebView)view.findViewById(R.id.PlanWebView);
return view;
   }

 }

其他推荐答案

您应该在onCreateView方法中使用下面的代码:

View view = inflater.inflate(layout,container,false);

WebView webview = (WebView) view.findViewById(R.id.webView);

return view;

本文地址:https://www.itbaoku.cn/post/2091045.html

问题描述

I am trying to create a WebView in a Fragment which should be loaded. I added it already to my xml File. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well?

I already tested the Solution from another Question, but it doesn't work.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    PlanWebView WebView = (PlanWebView) getView().findViewById(R.id.PlanWebView);
    // Unknown class: "PlanWebView";
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

}

推荐答案

Try this please , This is the OneFragment class , Webview needs a view which has to be inflated using xml

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

    View view=inflater.inflate(R.layout. fragment_one, container, false);
    mWebView = (WebView) view.findViewById(R.id.webview);
    mWebView.loadUrl("https://google.com");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    return view;
}

and xml for fragment_one

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

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

其他推荐答案

in Fragment you can declare your variables in onCreateView method.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
// Required empty public constructor
}

@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    }

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


 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
PlanWebView  = (WebView)view.findViewById(R.id.PlanWebView);
return view;
   }

 }

其他推荐答案

You should use code below in onCreateView method:

View view = inflater.inflate(layout,container,false);

WebView webview = (WebView) view.findViewById(R.id.webView);

return view;