Android dynamic layout compared to static layout, dynamic layout does not change the XML layout code, improve the efficiency, of course, can be ignored. Dynamic layout is mainly flexible, you can quickly modify the layout of the code directly, and directly using the control for business logic development. However, the amount of code is usually relatively large, maintenance is not static layout convenience. However, as an Android developer, mastering a certain dynamic layout skills, sometimes in the work can also improve the efficiency of code development.
In a dynamic layout, to achieve a layout, it is generally the first to create five major layout objects. These objects are then set to the property, and then the child layout or control is added to it.
Take Relativelayout as an example.
Relativelayout mlayout = new Relativelayout ();
Sets the child control Property object for the Relativelayout and sets its dimension style. Each groupview has a layoutprams that is used to set the child control to occur.
relativelayout.layoutprams params = new Relativelayout.layoutprams (layoutparams.wrap_content, layoutparams.wrap_content);
Add child controls
ImageView IV = new ImageView (getactivity ());
Iv.setimageresource (r.drawable.tab_icon_conversation_normal);
Sets the position property of the child control in Realtivelayout.
Params.addrule (relativelayout.center_in_parent, Relativelayout.true)//Add the attributes to IV
//Add IV to Mlayout
As you can see from the last sentence, the properties of the params object reference settings are all ImageView on the child control, and then the IV and the params are added to the realtivelayout.
Summary of the method of sorting Android dynamic layout
Absolute layout
Absolutelayout abslayout=new Absolutelayout (this);
Setcontentview (abslayout);
Button btn1 = New button (this);
Btn1.settext ("This is a button");
Btn1.setid (1);
Absolutelayout.layoutparams LP1 =
new Absolutelayout.layoutparams (
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
Abslayout.addview (BTN1, LP1);
Relative layout
Relativelayout relativelayout = new Relativelayout (this);
Setcontentview (relativelayout);
Absolutelayout abslayout=new Absolutelayout (this);
Relativelayout.layoutparams LP1 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Lp1.addrule (relativelayout.align_parent_top);
Lp1.addrule (Relativelayout.center_horizontal, relativelayout.true);
Relativelayout.addview (Abslayout, LP1);
Linear layout
LinearLayout ll = new LinearLayout (this);
EditText et = new EditText ();
Ll.addview (ET); Method of dynamically adding layouts 1. LinearLayout ll = (linearlayout) this.getlayoutinflater (). Inflate (R.layout.main1,null); Setcontentview (LL); LinearLayout ll2 = (linearlayout) this.getlayoutinflater (). Inflate (R.LAYOUT.MAIN2,LL); This main2 is added as a main1 to the Main1 root node//Dynamic Add Layout Method 2 AddView. LinearLayout ll = (linearlayout) this.getlayoutinflater (). Inflate (R.layout.main1,null); Setcontentview (LL); LinearLayout ll2 = (linearlayout) this.getlayoutinflater (). Inflate (R.layout.main2,null);
Ll.addview (LL2);