如何在 Fragment class 中加载 WebView?如何在 android 中加载 Webview?
How to Load WebView in Fragment class? How to load Webview in android?
我正在尝试在应加载的片段中创建 WebView。我已经将它添加到我的 xml 文件中。但是,findViewById 方法仅在我扩展 Activity class 时才有效。无论如何,我也可以在 Fragment 中使用它吗?
我已经测试了另一个问题的解决方案,但它不起作用。
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 class,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>
在 Fragment 中,您可以在 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;
我正在尝试在应加载的片段中创建 WebView。我已经将它添加到我的 xml 文件中。但是,findViewById 方法仅在我扩展 Activity class 时才有效。无论如何,我也可以在 Fragment 中使用它吗?
我已经测试了另一个问题的解决方案,但它不起作用。
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 class,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>
在 Fragment 中,您可以在 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;