1. Concept: when developing complex interfaces, especially tablet pages, the interface layout is often much more complex than the mobile phone layout. In this case, nested layout is required. At the same time, to achieve a certain effect, local pages must be dynamically changed. The most typical is to use multiple viewpager in a page. When the pages where viewpager is located are dynamic, You need to implement multi-layer linearlayout nesting. For example, for a tag page, the header is static, and the content is dynamically changed. At the same time, each content needs to be dynamically changed,CodeYou need to add multiple times for implementation.
2. The following is a simple case (this case only implements multi-layer nested addition. To achieve dynamic addition, you only need to change the content added each time through the control conditions .)
Padtestactivity. Java
Package Com. Devin; Import Android. App. activity; Import Android. OS. Bundle; Import Android. View. layoutinflater; Import Android. widget. linearlayout; Public Class Padtestactivity Extends Activity { /** Called when the activity is first created. */ @ Override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); layoutinflater Inflater = Getlayoutinflater (); // Add first page Linearlayout myfirst = (Linearlayout) Inflater. Inflate (R. layout. First, Null ). Findviewbyid (R. Id. myfirst); linearlayout layoutmain = (Linearlayout) findviewbyid (R. Id. layoutmain); layoutmain. removeallviews (); layoutmain. addview (myfirst ); // Show the page first // Add second page Linearlayout mysecond = (Linearlayout) Inflater. Inflate (R. layout. Second, Null ). Findviewbyid (R. Id. mysecond); linearlayout layoutfirst = (Linearlayout) findviewbyid (R. Id. layoutfirst); layoutfirst. addview (mysecond ); // Add Third page Linearlayout mythird = (Linearlayout) Inflater. Inflate (R. layout. Third, Null ). Findviewbyid (R. Id. mythird); linearlayout layoutsecond = (Linearlayout) findviewbyid (R. Id. layoutsecond); layoutsecond. addview (mythird );}}
Layout code
Main. xml
<? XML version = "1.0" encoding = "UTF-8" ?> < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" Android: Orientation = "Vertical" > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Main page" /> < Linearlayout Android: ID = "@ + ID/layoutmain" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" > </ Linearlayout > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Main page" /> </ Linearlayout >
First. xml
<? XML version = "1.0" encoding = "UTF-8" ?> < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: ID = "@ + ID/myfirst" Android: layout_width = "Match_parent" Android: layout_height = "Match_parent" Android: Orientation = "Vertical" > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "First page" /> < Linearlayout Android: ID = "@ + ID/layoutfirst" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" > </ Linearlayout > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "First page" /> </ Linearlayout >
Second. xml
<? XML version = "1.0" encoding = "UTF-8" ?> < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: ID = "@ + ID/mysecond" Android: layout_width = "Match_parent" Android: layout_height = "Match_parent" Android: Orientation = "Vertical" > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Second page" /> < Linearlayout Android: ID = "@ + ID/layoutsecond" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" > </ Linearlayout > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Second page" /> </ Linearlayout >
Third. xml
<? XML version = "1.0" encoding = "UTF-8" ?> < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: ID = "@ + ID/mythird" Android: layout_width = "Match_parent" Android: layout_height = "Match_parent" Android: Orientation = "Vertical" > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Third page" /> < Linearlayout Android: ID = "@ + ID/layoutthird" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" > </ Linearlayout > < Textview Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Text = "Third page" /> </ Linearlayout >