Android uses include in layout xml to perform static loading. androidlayout

Source: Internet
Author: User
Tags control label

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 > 

FoundincludeThe 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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.