< merge / > and < ViewStub / > for android layout optimization

After the activity is initialized. After executing onCreate, setContentView() will be executed. Even a beginner will know this.

But there is a small problem here: if the nesting of layout is too complex, there will be problems such as excessive drawing or slow loading. Then you need to optimize the layout.

Then there are many ways to optimize,

For example, constraint layout - ConstraintLayout :
ConstraintLayout is a ViewGroup that can be used in Android systems above Api9. Its appearance is mainly to solve the problem of excessive nesting of layouts, and to position and adjust widgets in a flexible way. Starting from Android Studio 2.3, the official template uses ConstraintLayout by default.

Of course, there is no introduction to ConstraintLayout here. Students who don’t know how to use Baidu, today I’m going to talk about <merge /> and <ViewStub /> . Not much nonsense. Start text:

One of: <merge />

Application scenario: For example, there is no nesting to the third layer in the layout. That is to say, there is only the parent layout ViewGroup and the view below, and there is no deeper nesting, as shown in the figure:
insert image description here
< merge /> can be used at this time:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher_background" />
</merge>

Of course, it can be achieved by using any viewGroup, but, but, but:

this means that when there is any place to include this layout, the content contained in the merge tag will be directly filled into the include position, and no more will be added The extra layout structure

is in human terms: saving performance .

One of: <ViewStub />

ViewStub is a lightweight view that is invisible and does not take up layout space. It also takes up very little resources. It will only be initialized when Inflate, that is, lazy loading.

Features:

  • It can only be Inflate once, and then the ViewStub object will be empty.
  • Only one layout file can be inflated, and no specific view is supported (you can separate the view into a layout file and use it in conjunction with merge according to the situation) :

Use as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <ViewStub
        android:id="@+id/vs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/test1" />
</LinearLayout>

test1 in the code is the lazy loaded layout. It is not shown here.

Loading method:

 vs?.inflate()

This article only introduces the special effects and how to use them. For more details, please use Baidu.
END

Guess you like

Origin blog.csdn.net/lixinxiaos/article/details/123845888