findViewById in Fragment

后端 未结 30 2942
终归单人心
终归单人心 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:37

    Inside onCreateView method

    1) first you have to inflate the layout/view you want to add eg. LinearLayout

    LinearLayout ll = inflater.inflate(R.layout.testclassfragment, container, false);
    

    2) Then you can find your imageView id from layout

    ImageView imageView = (ImageView)ll.findViewById(R.id.my_image);
    

    3)return the inflated layout

    return ll;
    

提交回复
热议问题