Android: How to dynamically include a XML Layout?

前端 未结 2 790
逝去的感伤
逝去的感伤 2020-12-14 00:08

I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.

I would like to be able

相关标签:
2条回答
  • 2020-12-14 00:31

    You could try using a ViewStub and just change which layout it is going to inflate programatically.

    This answer discusses using one ViewStub.

    0 讨论(0)
  • 2020-12-14 00:33
    <RelativeLayout android:id="@+id/rl" ...
    

    In your code:

    // get your outer relative layout
    RelativeLayout rl = (RelativeLayout) findById(R.id.rl);
    
    
    // inflate content layout and add it to the relative layout as second child
    // add as second child, therefore pass index 1 (0,1,...)
    
    LayoutInflater layoutInflater = (LayoutInflater) 
            this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); 
    
    0 讨论(0)
提交回复
热议问题