2015-12-13 51 views
-5

嗨,大家好,我有一個問題。 如何在Fragment內使用WebView? 我知道如何在Activity中使用WebView,但我不知道如何在Fragment中執行此操作。 因爲在Fragment中不能使用FindViewById如何在片段l中使用WebView?

感謝您的幫助

弗洛帝國

+4

歡迎堆棧溢出。請看[問]。 –

+0

請回答我的問題 –

回答

1

你必須讓你的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; 
} 
+0

謝謝你幫助我:-)祝你有美好的一天 –

0

可以使用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; 
} 

希望,它幫助。

+0

非常感謝你。祝你有美好的一天 –