findViewById in Fragment

后端 未结 30 2357
终归单人心
终归单人心 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 07:40

    Use gradle skeleton plugin, it will automatically generate the view holder classes with the reference to your layout.

    public class TestClass extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            MyLayout myLayout = new MyLayout(inflater, container, false);
            myLayout.myImage.setImageResource(R.drawable.myImage);
            return myLayout.view;
        }
    }
    

    Now assuming you had an ImageView declared in your my_layout.xml file, it will automatically generate myLayout class for you.

提交回复
热议问题