Summary of Android dynamic Layout

  • 2020-12-19 21:11:20
  • OfStack

Compared with static layout, dynamic layout does not need to change xml's layout code, which improves the efficiency by 1 point. Of course, it can be ignored. Dynamic layout is mainly flexible, you can quickly modify the layout directly in the code, and direct use of controls for business logic development. But the amount of code is usually large, and maintenance is not as convenient as static layout. However, as an android developer, mastering 1-point dynamic layout skills can sometimes improve 1-point code development efficiency at work.

In dynamic layout, to achieve 1 layout, 1 usually creates 5 layout objects first. These objects are then set to properties and then sublayouts or controls are added to them.

Take RelativeLayout for example.


 RelativeLayout mLayout = new RelativeLayout();
    // Set up the RelativeLayout Sets the size style of the child control property object. each GroupView There are 1个LayoutPrams, Is used to set the occurrence of child controls. 


    RelativeLayout.LayoutPrams params = new RelativeLayout.LayoutPrams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // increase   Child controls 
    ImageView iv = new ImageView(getActivity());
    iv.setImageResource(R.drawable.tab_icon_conversation_normal);
    // Sets the child control in RealtiveLayout Position attribute in. 
    params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // to iv  Increase the attributes 
     // will iv , increased to mLayout In the 
    mLayout .addView(iv, params); 

As you can see from the last sentence, params object references set the properties on the ImageView child control, and then add iv and params1 blocks to RealtiveLayout.

Collate the android dynamic layout method summary

// 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);
// Dynamically add layout methods 1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); // such  main2  As a  main1 A child of the layout   Added to  main1 the   Under the root node 
// Dynamically add layout methods 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);

Related articles: