Android dynamic layout compared to static layout, dynamic layout does not have to transform the XML layout code, improve a certain efficiency, of course, can be ignored do not remember. Dynamic layout is mainly flexible, can quickly modify the layout directly in the code, and directly use the control for business logic development. However, the code size is usually larger, and maintenance is not static layout convenient. However, as an Android developer, mastering a certain dynamic layout skills, and sometimes in the work can also improve the efficiency of a certain code development.
In a dynamic layout, to implement a layout, you typically create the first five layout objects. The properties are then set for the objects, and then the child layouts or controls are added to them.
Take Relativelayout as an example.
Relativelayout mlayout = new Relativelayout ();
Sets the child control Property object for 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);
Adding 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 attribute to IV
Add IV to Mlayout
mlayout . AddView (iv, params);
From the last sentence, it can be seen that the params object reference set properties are ImageView on this child control, and then add the IV and the params piece into the realtivelayout.
Android Development-Dynamic layout notes