Android development-Dynamic Layout note, android note
Compared with static la S, android dynamic la s do not need to change xml to layout code, which improves the efficiency. Of course, you can ignore it. The Dynamic Layout is mainly flexible. You can quickly modify the layout in the Code and directly use controls for business logic development. However, the amount of code is usually large, so it is not convenient to maintain the static layout. However, as an android developer, mastering certain dynamic layout skills can also improve code development efficiency at work.
In the dynamic layout, to achieve a layout, it is generally the first to create five layout objects. Set the attributes of these objects, and then add sub-la s or controls to them.
Take RelativeLayout as an example.
RelativeLayoutMLayout= New RelativeLayout ();
// Set the child control property object of RelativeLayout and set its size style. Each GroupView has a LayoutPrams, which is used to set the child control.
RelativeLayout. LayoutPramsParams= New RelativeLayout. LayoutPrams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
// Add a child Control
ImageView iv = new ImageView (getActivity ());
Iv. setImageResource (R. drawable. tab_icon_conversation_normal );
// Set the position attribute of the subcontrol in RealtiveLayout.
Params. addRule (RelativeLayout. CENTER_IN_PARENT, RelativeLayout. TRUE); // adds an attribute to iv
// Add iv to mLayout
MLayout. AddView (iv, params );
From the last sentence, we can see that all the attributes set by the params object reference apply to the sub-control ImageView, and then add iv and params to RealtiveLayout.