findViewById in Fragment

后端 未结 30 2356
终归单人心
终归单人心 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条回答
  •  Happy的楠姐
    2020-11-21 07:17

    In fragments we need a view of that window so that we make a onCreateView of this Fragment.

    Then get the view and use it to access each and every view id of that view elements..

      class Demo extends Fragment
        {
            @Override
            public View onCreateView(final LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
            {
                View view =inflater.inflate(R.layout.demo_fragment, container,false);
                ImageView imageview=(ImageView)view.findViewById(R.id.imageview1);
    
                return view;
            }
        }
    

提交回复
热议问题