findViewById in Fragment

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

    1) Declare your layout file.

    public View onCreateView(LayoutInflater inflater,ViewGroup container, 
                                     Bundle savedInstanceState) {
        return inflate(R.layout.myfragment, container, false);
    }
    

    2)Then, get the id of your view

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView nameView = (TextView) view.findViewById(R.id.textview1);
    }
    

提交回复
热议问题