如何在 Fragment l 中使用 WebView?

How to use WebView inside a Fragment l?

大家好,我有一个问题。 如何在 Fragment 中使用 WebView? 我知道如何在 Activity 中使用 WebView,但我不知道如何在 Fragment 中使用它。 因为你不能在 Fragment.

中使用 FindViewById

感谢帮助

弗洛帝国

可以片段中使用findViewById()方法。比如在onCreateView方法中:

@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
    View fragmentView = layoutInflater.inflate(R.layout.your_fragment, container, false);

    // and you can use findViewById() method on fragmentView

    View v = fragmentView.findViewById(R.id.your_view);
    ... 
    return fragmentView;
} 

希望对您有所帮助。

你必须让你的onCreateView像这样

@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
    View v = layoutInflater.inflate(R.layout.my_fragment, container, false);
    WebView webview = (WebView) v.findViewById(R.id.webview_id);
    // other stuff
    return v;
}