<include> <merge> <view stub> use and why

When using the layout, you can consider include merge view stub

Each uses the following and optimized points

Include
If some layouts are repeated, you can wrap the repeated ones with include and use them as a module

Write the module test.xml first

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 
    <TextView
        android:id="@+id/tv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:textColor="@android:color/black"
        android:textSize="13sp" />
 
</LinearLayout>

post-exploitation

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test_include"
        android:textColor="@android:color/black"
        android:textSize="13sp" />
 
    <include layout="@layout/test" />
 
</LinearLayout>

merge uses

Merge is not a ViewGroup, nor is it a View. It is equivalent to declaring some views, waiting to be added. After the
merge tag is added to the parent container, all views under the merge will be added to the parent container, that is, all views under the merge will be Follow the layout rules of the parent container

it's here

merge.xml
insert image description here

It is written that the background color and vertical placement will not take effect

The following two child views textview will only follow the parent container that references merge.xml,
which is the horizontal arrangement of the LinearLayout here
insert image description here

The optimization point of merge is that, for example, if you have a layout, the root layout is a FrameLayout.
insert image description here

Then your layout will be added to other layouts. And the added layout parent container is also FrameLayout
insert image description here
, so you can use merge to remove FrameLayout in the sub-module layout and write it as merge. Use the merge feature to drop a layer of FrameLayout
insert image description here
merge Notes:

  • Merge must be placed on the root node of the layout file, and all properties set for merge are invalid
  • merge is not a ViewGroup, nor a View, it is equivalent to declaring some views, waiting to be added.
  • Because the merge tag is not a View, when rendering through the LayoutInflate.inflate method, the second parameter must specify a parent container, and the third parameter must be true, that is, a parent node must be specified for the view under merge.
  • After the merge tag is added to the parent container, all views under the merge will be added to the parent container, that is, all views under the merge will follow the layout rules of the parent container

The use of view stub
also introduces a sub-module layout in the layout layout
insert image description here

Of course, you can also write your layout directly in the view stub
insert image description here

The function of the view stub is that you can use this view stub to control when the internal sub-view is loaded,
that is, the layout of activity_main.xml will not create the view under the view stub tag when creating the view in LayoutInflater.inflate, and wait until it is executed

 ViewStub viewStub = findViewById(R.id.vs_test);
 if (viewStub != null) {
     //viewStub.setVisibility(View.VISIBLE);
     View inflated = viewStub.inflate();
     View view = inflated.findViewById(R.id.tv_test);
 }

That is, after using the viewStub object to call its
viewStub.inflate() or viewStub.setVisibility(View.VISIBLE), the layout under the ViewStub tag will be displayed, and viewStub.inflate() will not be executed, and the page will not be displayed or displayed. Draw the layout under the ViewStub tag.

This is still different from the view setting gone. Setting gone is hidden, but the view will still create
a viewstub. The view will not be created, so it will not be displayed. Calling viewStub.inflate() will be created and displayed.
This delay is manually displayed. Create data that can speed up the startup of the layout to improve performance

Note: ViewStub can only be Inflate or setVisibility(View.VISIBLE) once. After inflate, the ViewStub object will be empty, and you can no longer use ViewStub to control the view under other labels.

Guess you like

Origin blog.csdn.net/weixin_43836998/article/details/126037116