Implementation of multi-layer dynamic nested layout in Android

Source: Internet
Author: User

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  > 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.