[英]Android - Displaying WebView in Fragment
So I have a fragment with the following in the XML layout file: 因此,我在XML布局文件中有一个包含以下内容的片段:
<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"
tools:context="com.example.dwinnbrown.myapplicationv20.MainFragment">
<!-- TODO: Update blank fragment layout -->
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I am now trying to load a url into the web view through the java file associated with the fragment. 我现在正尝试通过与片段关联的java文件将网址加载到网络视图中。 However I am running into an error "cannot resolve findViewByID(int)".
但是我遇到一个错误“无法解析findViewByID(int)”。 Any ideas?
有任何想法吗?
Here is the code I am trying to use for the fragment: 这是我要用于片段的代码:
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;
/**
* A simple {@link Fragment} subclass.
*/
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
}
Do Something like this 做这样的事情
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView view = (WebView) rootView.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return rootView ;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView wv = (WebView) view.findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
}
Below is the xml for fragment: 以下是片段的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webview">
</WebView>
</RelativeLayout>
And try this for fragment class 并尝试此片段类
public class MainFragment extends Fragment {
WebView webView;
public MainFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webView=(WebView)getActivity().findViewById(R.id.webview);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("your_url");
webView.setWebViewClient(new Mybrowser());
if (savedInstanceState != null)
webView.restoreState(savedInstanceState);
else
webView.loadUrl("your_url");
}
private class Mybrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.