The include tag in the Android layout uses
Recently in the layout, there are a lot of pages are common features, such as the title: a similar style!
As shown in the following:
If you give each page a separate title layout that would be too much trouble, if you can write a title layout, other pages reuse that much better!
This time, <include>
it is a grand debut!
Write a title for the layout title.xml
:
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent"android:layout_height="Wrap_content" android:background="@drawable/navigation_bar" > <ImageViewandroid:id="@+id/img_backward"android:layout_alignparentleft ="true"android:layout_centervertical="true"android:layout_marginleft= "25DP" android:layout_gravity="center"android:layout_width="Wrap_content" android:layout_height="Wrap_content"android:src="@drawable/backward" /> <TextView Android:id="@+id/tv_title" Android:layout_width="Wrap_content" Android:layout_height="Wrap_content" android:textsize="20SP" android:gravity="Center" android:layout_centerinparent="true" android:layout_gravity="Center" Android:textcolor="@color/white" Android:text="@string/tiltename"/></relativelayout>
On top of a few other similar pages <include>
title.xml
:
<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="Vertical" > <!--titles -- <includeandroid:id="@+id/title_res"layout="@layout/title" / > <!--interface Content -- <framelayoutandroid:id="@+id/app_content"android:layout_width= "Match_parent" android:layout_height="match_parent" /> </linearlayout>
It is important to note that in Google's official documentation there is a <include ...>
last word:
However, if you want-to-override layout attributes using the tag, you must override both Android:layout_height and Android : Layout_width in order for other layout attributes to take effect.
The meaning is:
If you want to <include ...>
use attributes in a tag layout_**
, you must include and in the tags include
android:layout_height
android:layout_width
, otherwise the layout_**
properties will not take effect!
Summarize
In short, the label is to reuse the existing layout, no need to write redundant code! ———— multiplexing Layouts
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The include tag in the Android layout uses