Optimized layout of merge and include developed by Android

  • 2021-07-22 11:32:51
  • OfStack

merge combined with include to optimize the layout of android, the effect is unknown, and there are great limitations in personal feeling, but I still know about it and record it.

Layout files must have root nodes, but too much layout nesting in android will cause performance problems, so we can use merge as the root node when using include nesting, which can reduce layout nesting and improve display speed.


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

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" Zhang 3" />

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" Li 4" />

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" Wang 5" />

</merge>

The above interface will be automatically nested into the following file when it is displayed.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal"
 tools:context="com.example.viewstub.MainActivity" >

 <include layout="@layout/top"/>
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/hello_world" />

 <Button
  android:id="@+id/toggle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="onClick"
  android:text=" Display / Hide " />

 <ViewStub
  android:id="@+id/vs"
  android:layout_margin="50dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:inflatedId="@+id/inflated_id"
  android:layout="@layout/view_stub_layout" />

</LinearLayout>

Why do I say that this limitation is relatively large? Because the use of space display in merge will be displayed in the way of main layout file. For example, if the main layout here is linearlayout and is arranged horizontally, then the elements in merge are also arranged horizontally after being displayed, but what if I want the elements in merge to be arranged vertically? Sorry, I can't do it.

Original link: http://blog.csdn.net/u012702547/article/details/47133647

The above is the whole content of this article, hoping to help everyone learn Android software programming.


Related articles: