Add view dynamically to android

Posted by barrylee on Mon, 03 Jan 2022 18:17:25 +0100

Most of the time, we use xml for layout, but sometimes we also use dynamic layout, especially in some large projects.
So today we'll learn some two ways to add layout dynamically, namely

  • Add xml layout dynamically

    • In addition, write an xml layout, and then let it load on its main layout through java
  • Add java layout dynamically

    • Write a layout in java, and then let it load on its main layout in java

1, Add xml layout dynamically

step

The first step is to construct the container

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(   
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

The second step is to construct the xml file

Here, we can add a button to the main layout to click and load the xml layout. I will write a TextView in the added layout. Because it is relatively simple, it will not be posted here

Step 3: construct layoutinflator

When it comes to addview, you first need to understand the layoutinflator class. The main function of this class is to convert the layout expressed in xml into View. For ease of understanding, we can compare it with findViewById(). Both of them instantiate an object. The difference is that findViewById() instantiates a specific widget control under the xml layout file, while layoutinflator instantiates it through the xml layout file under res/layout /.

There are three ways for layoutinflator

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);or

LayoutInflater inflater = LayoutInflater.from(Activity.this);or

LayoutInflater inflater = getLayoutInflater();

The three methods are essentially the same.

Step 4: load the layout with inflate()

Use layoutinflator Inflate() converts the LayOut file to VIew.

View view = inflater.inflate(R.layout.block_gym_album_list_item, null);
this.addContentView(view,lp);

Detailed introduction to the first step display layout

Changing the Layout is mainly realized by changing layoutparams, which is inherited from Android View. ViewGroup. LayoutParams. It is equivalent to a Layout information package, which encapsulates the position, height, width and other information of the Layout. It should be noted that the parent container of the view to be modified is of different types, such as FrameLayout, LinearLayout, RelativeLayout, etc., because different types of layoutparams are different.

The above code is the same as the following xml layout code:

<LinearLayout>
        <View
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"/>
</LinearLayout>

Change layout position

addRule can pass in different layout parameters, such as addRule(RelativeLayout.BELOW, R.id.XXX); Indicates that the View is under XXX. Common layout parameters are as follows:

layoutParams.alignWithParent = true  If the corresponding sibling element cannot be found, the parent element is used as the reference
RelativeLayout.CENTER_HORIZONTAL   Center horizontally in parent control
RelativeLayout.CENTER_VERTICAL   Center vertically in parent control
RelativeLayout.CENTER_IN_PARENT  Completely centered relative to the parent control
RelativeLayout.ALIGN_PARENT_BOTTOM  Close to the lower edge of the parent control
RelativeLayout.ALIGN_PARENT_TOP  Close to the upper edge of the parent control
RelativeLayout.ALIGN_PARENT_LEFT Close to the left edge of the parent control
RelativeLayout.ALIGN_PARENT_RIGHT  Close to the right edge of the parent control
RelativeLayout.ABOVE The second parameter above an element is required to be the name of an element ID
RelativeLayout.BELOW The second parameter below an element needs to be the name of an element ID
RelativeLayout.LEFT_OF On the left side of an element, the second parameter is required to be the name of an element ID
RelativeLayout.RIGHT_OF  On the right side of an element, the second parameter needs to be the name of an element ID
RelativeLayout.ALIGN_TOP The alignment between the upper edge of this element and the upper edge of an element requires the second parameter to be the upper edge of an element ID
RelativeLayout.ALIGN_BOTTOM The alignment of the upper edge of this element with the lower edge of an element requires the second parameter to be the lower edge of an element ID
RelativeLayout.ALIGN_LEFT  The alignment of the upper edge of this element with the left edge of an element requires the second parameter to be the left edge of an element ID
RelativeLayout.ALIGN_RIGHT  The alignment of the upper edge of this element with the right edge of an element requires the second parameter to be the right edge of an element ID
RelativeLayout.ALIGN_BASELINE   The alignment between the baseline of this element and the baseline of an element requires the second parameter to be the baseline of an element ID

2, Add java layout dynamically

Let's take TextView as an example. In fact, dynamically adding java layout and xml layout are similar. It's nothing more than writing out controls in java and adding them to the layout

The first step is to construct the container

It's the same as the xml layout above, so it's not posted here

The second step is to construct the control layout framework

In fact, it is the same as the second step of xml, but xml has written the layout of the outermost layer, so we also need to write a layout in java to put controls in it

LinearLayout view = new LinearLayout(this); 
        view.setLayoutParams(lp);//Set layout parameters 
        view.setOrientation(LinearLayout.HORIZONTAL);// Set the Linearlayout of the child View / / to the vertical layout 
        //Defines the layout of two elements in a child View 
        ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams( 
                ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT); 

Step 3 add control

In fact, there is nothing to say about this step, that is, the way to add controls in xml is changed to add controls in java

TextView tv1 = new TextView(this); 
        tv1.setLayoutParams(vlp);//Set the layout of TextView 
        tv1.setText("full name:");

The fourth part adds the control to the container

 view.addView(tv1);//Add TextView to child View 

7 likes

Android



Author: Hou egg_
Link: https://www.jianshu.com/p/06c9c6685108
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.