Recently, many experiments have been conducted on the modification of controls in Android. Due to the company's many demands, many controls have to be rewritten. Program goals: efficient, lightweight, clear, and standardized
You can use either of the following methods to complete Dynamic Layout loading:
Method 1: static Master Layout dynamically loads static sub-Layout
First, construct the sub-layout: main2
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! -- Layout can be any definition, here take the linear layout example, there are 2 button elements --> <br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: id = "@ + ID/menubar" <br/> Android: Background = "@ drawable/menubar" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> <! -- Button 1 --> <br/> <imagebutton Android: Id = "@ + ID/button1" <br/> Android: src = "@ drawable/btn1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> </imagebutton> <br/> <! -- Button 2 --> <br/> <imagebutton Android: Id = "@ + ID/button2" <br/> Android: src = "@ drawable/btn2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> </imagebutton> <br/> </linearlayout>
Then build the Master Layout: Main
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/background" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Background = "@ drawable/background"> <br/> <! -- To set a container box for the sub-layout in the main layout, you can specify the location of the container here, which is the key part --> <br/> <linearlayout Android: id = "@ + ID/box" <br/> Android: layout_alignparentbottom = "true" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_centerhorizontal = "true"> <br/> </linearlayout> <br/> </relativelayout>
Finally, load the sub-layout in the program:
Public class backgroundtest extends activity {<br/>/** called when the activity is first created. */<br/> // Add the child layout to the master layout as a view. <br/> private view mbarview; <br/> // load the view of the sub-layout to the container of the main layout <br/> private linearlayout mlinearlayout; <br/> // key content <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // display the Master Layout <br/> setcontentview (R. layout. main); <br/> // load the sub-layout <br/> mbarview = view. inflate (this, R. layout. main2, null); <br/> // find the container <br/> mlinearlayout = (linearlayout) findviewbyid (R. id. box); <br/> // Add view to end <br/> mlinearlayout. addview (mbarview); <br/>}
Method 2: static Master Layout dynamically loads Dynamic Layout
First, construct your own child layout as above;
Then build your custom Layout class:
Public class menulandscapelinearlayout extends linearlayout {<br/> // constructor <br/> Public menulandscapelinearlayout (context) {<br/> super (context ); <br/> // todo auto-generated constructor stub <br/> // load the required attributes and the sub-layout of method 1 <br/> (activity) getcontext ()). getlayoutinflater (). inflate (R. layout. main2, this); <br/> // You can encapsulate many methods here <br/>}< br/>}
Finally, it can be dynamically instantiated and loaded in the program:
Public class backgroundtest extends activity {<br/>/** called when the activity is first created. */<br/> private linearlayout mlinearlayout; <br/> // declare a layout view object. <br/> private menulandscapelinearlayout mmenulandscapelinearlayout; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // load the Master Layout <br/> setcontentview (R. layout. main); <br/> // find the container <br/> mlinearlayout = (linearlayout) findviewbyid (R. id. box); <br/> // instantiate a sub-view <br/> mmenulandscapelinearlayout = new menulandscapelinearlayout (this); <br/> // Add to container <br/> mlinearlayout. addview (mmenulandscapelinearlayout); <br/>}< br/>}
Now, we have completed two forms of dynamic loading of sub-layout, many of which can be considered, such as encapsulating common events and resources, thus saving code and resources;
I hope to help you optimize your programs.