findViewById in Fragment

后端 未结 30 2826
终归单人心
终归单人心 2020-11-21 06:39

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById<

30条回答
  •  深忆病人
    2020-11-21 07:16

    You could also do it in the onActivityCreated Method.

    public void onActivityCreated(Bundle savedInstanceState) { 
          super.onActivityCreated(savedInstanceState);
    }
    

    Like they do here: http://developer.android.com/reference/android/app/Fragment.html (deprecated in API level 28)

    getView().findViewById(R.id.foo);
    

    and

    getActivity().findViewById(R.id.foo);
    

    are possible.

提交回复
热议问题