add layout as a view in Android

前端 未结 3 1449
清歌不尽
清歌不尽 2021-02-20 02:44

I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.

As layout is pret

相关标签:
3条回答
  • 2021-02-20 03:11

    Use

    LayoutInflater factory = LayoutInflater.from(this);
    View myView = factory.inflate(R.layout.my_layout_id, null);
    

    then use addView(myView)

    0 讨论(0)
  • 2021-02-20 03:12

    If your view is a ViewGroup (layout) You can use InflaterService.inflate(id, ViewGroup) and set the ViewGroup, it will set the current child(s) with the content of your xml.

    0 讨论(0)
  • 2021-02-20 03:15

    U can also reduce this to oneline code;

      View view = View.inflate(getActivity, R.layout.my_layout,null);
    

    then add to your view.

    0 讨论(0)
提交回复
热议问题