Android uses include in layout xml to perform static loading. androidlayout
Android uses include in layout xml to perform static loading.
Include static loading:
Not only can layout be loaded, but also controls can be loaded (control label names must be at the outermost layer)
The include tag has a layout attribute that is specifically used for loading.
In the layout style definition of Android, xml files can be used for convenient implementation. Sometimes, to reuse modules, the include tag can be used for this purpose. For example:
<include layout="@layout/otherlayout"></div>
The official website for android development is described here.
It is mentioned that:
Similarly, you can override all the layout parameters. This means that any android: layout _ * attribute can be used with<include>
Tag.
This means that any android: layout _ * attribute can be applied to tags.
Use the following code:
<Relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/somestring" android:id="@+id/top" /> <include layout="@layout/otherlayout" android:layout_below="@id/top" /></Relativelayout >
Foundinclude
The otherlayout is not under the TextView id/top as expected, but ignores the android: layout_below attribute. Google found that many people encountered similar problems.
The solution is to package a LinearLayout layer outside the include, as shown below:
<Linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/top" > <include layout="@layout/otherlayout"></Linearlayout >
A better solution is found on Statckoverflow: the answer is: layout must be reloaded at the same time.Width and layoutThe height attribute. Other layout _ * attributes will take effect. Otherwise, the layout _ * attribute will be ignored. The above example should be written as follows:
<include layout="@layout/otherlayout"> android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@id/top" />
In addition, on the xml reuse, you can also use merge labels, merge labels are mainly used to optimize the display, reduce the View tree hierarchy, can refer to here: https://developer.android.com/resources/articles/layout-tricks-merge.html, translation version here: http://apps.hi.baidu.com/share/detail/20871363
Original article: http://www.race604.com/using-include-in-layout/