Use and precautions for include tags in android

Source: Internet
Author: User

Preface

?? The include tag enables the layout to reference another layout in one layout, which is often appropriate for apps with complex interface layouts, with common layouts for different interfaces, such as the top layout of an app, the sidebar layout, the bottom tab bar layout, The layout of each item in the ListView and GridView is extracted from the layout of the same app with multiple interfaces and referenced by the include tag to reduce the complexity of the layout. You can also do layout reuse (the layout changes only need to modify one place).

How to use

?? The include tag is very simple to use, just need to refer to other layouts in the layout file, use layout= "@layout/child_layout" on it:

<include layout= "@layout/titlebar"/>

?? For example, theincludevoiceCtrlBarLayout.xml is a common layout that can be extracted:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 61DP "android:orientation=" Horizontal "and        Roid:layout_alignparentbottom= "true" > <!--play, pause--<button android:id= "@+id/voicebtnid" Android:layout_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "Android:b" ackground= "@drawable/voice_btn_selector" android:gravity= "center" android:onclick= "OnClick" android:t ext= "@string/voice_stop" android:textcolor= "@android: Color/white" android:textsize= "24sp"/> <!-- Secant--<view android:layout_width= "0DP" android:layout_height= "Match_parent" Android:layout_ weight= "1" android:background= "@android: Color/black"/> <!--listen to the tape--<button android:id= "@ +id/listenbtnid "Android:layOut_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "android:background=" @drawable/listen_btn_selector "android:gravity=" center "android:onclick=" OnClick "android:text=" @stri Ng/voice_listen "android:textcolor=" @android: Color/white "android:textsize=" 24sp "/> <!--split Line--&gt    ;        <view android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" Android:background= "@android: Color/black"/> <!--next--<button android:id= "@+id/nextbtnid        "Android:layout_width=" 0DP "android:layout_height=" match_parent "android:layout_weight=" 100 " android:background= "@drawable/next_btn_selector" android:gravity= "center" android:onclick= "OnClick" a ndroid:text= "@string/voice_next" android:textcolor= "@android: Color/white" android:textsize= "24SP"/></ Linearlayout>

?? Make the following calls where you need to use the shared layout:

<relativelayout    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:background= "@drawable/background_bottom_layer" >    <relativelayout        android:layout_ Width= "Match_parent"        android:layout_height= "match_parent"        android:layout_margin= "10DP"        android: background= "@drawable/background_top_layer" >        <include layout= "@layout/include_voice_text_bar_layout" />        <include layout= "@layout/include_voice_ctrl_bar_layout"/>    </relativelayout></ Relativelayout>

Precautions
  • include and other component tags (relativelayout, linearlayout, TextView, etc.) You can use the Layout property to set the width height and position of the layouts file, but be aware that the android:layout width and Android must be replicated: Layoutheight property to use other properties (such as Android:layout grivity, android:layout align ..., android:id, etc.) to avoid include refers to the layout effect of sub-component properties in layout that affect include :

    <relativelayout    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:background= "@drawable/background_bottom_layer" >    <relativelayout        android:layout_ Width= "Match_parent"        android:layout_height= "match_parent"        android:layout_margin= "10DP"        android: background= "@drawable/background_top_layer" >        <include layout= "@layout/include_voice_text_bar_layout" />        <include            android:layout_width= "match_parent"            android:layout_height= "61DP"            Android : Layout_alignparentbottom= "true"            layout= "@layout/include_voice_ctrl_bar_layout"            />    </ Relativelayout></relativelayout>

  • It is recommended that the include tag call layout settings, location, ID, and so on, be placed in the root tag of the call layout, which avoids the problems caused by improper setting of the include tag (previously encountered to the include after you set the Android:id property on the label, the program instantiates the failure of the component in the child layout):

?? This should be the case:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/bottombarlayoutid "    android:layout_width=" match_parent "    android:layout_ height= "61DP"    android:orientation= "horizontal"    android:layout_alignparentbottom= "true" >    ... </LinearLayout>

<include layout= "@layout/include_voice_ctrl_bar_layout"/>

Rather than this:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" > ...    </LinearLayout>

<include    android:id= "@+id/bottombarlayoutid"    android:layout_width= "match_parent"    android:layout _height= "61DP"    android:layout_alignparentbottom= "true"    layout= "@layout/include_voice_ctrl_bar_layout"    />

References
    • Does Android XML Layout ' s ' include ' Tag really work?

    • Re-using Layouts with

Use and precautions for include tags in android

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.