Reuse layout files with <include/> labels
Although Android provides tiny and reusable interactive elements through various built-in controls, you may need to reuse large
Components-some specific layout files. For more efficient reuse of layout files, you can use <include/> and <merge/>
Label to add other layout files to the current layout file.
Reusing layout files is a particularly powerful method that allows you to create reusable layout files. For example,
Button panel, or a Progressbar with text instructions. Reusing layout files also means that any elements in your application can be retrieved from
Complicated layout files are extracted for separate management. What you need to do is add these independent layout files (because they are reusable ).
Therefore, when you create an independent UI component through a custom View, you can reuse the layout file to make things easier.
1. Create a reusable layout File
If you already know how to reuse the layout, create and define the layout file (named with the suffix ". xml ). For example
The G-Kenya codelab layout file defines a custom title (titlebar. xml) to be used in each Activity:
Reusable la s are added to other layout files. Therefore, each of its root views should be accurate (exactly.
2. Use the <include/> label
Use the <include/> label where you want to add these la S. For example, the following is a layout file from G-Kenya codelab,
It reuses the "title bar" file listed above. The layout file is as follows:
[Java]
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ color/titlebar_bg">
<ImageView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/gafricalogo"/>
</FrameLayout>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ color/titlebar_bg">
<ImageView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/gafricalogo"/>
</FrameLayout>
You can also define a special identifier for the root View of the added layout file in the <include/> node and override all layout parameters (any
Attribute prefixed with "android: layout ). For example:
[Java]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/app_bg"
Android: gravity = "center_horizontal">
<Include layout = "@ layout/titlebar"/>
<TextView android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: padding = "10dp"/>
...
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/app_bg"
Android: gravity = "center_horizontal">
<Include layout = "@ layout/titlebar"/>
<TextView android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: padding = "10dp"/>
...
</LinearLayout>
3. Use the <merge/> label
When another layout is used in the layout file, the <merge/> label can remove redundant view elements at the layout level. For example, if your
The main layout file is a LinearLayout that vertically contains two views. This layout can be reused in other layout S.
The layout file requires a root View (otherwise, the compiler will prompt an error ). However, add a LinearLayout
As the root View, a vertical LinearLayout contains another vertical LinearLayout. Embedded LinearLayout can only slow down
UI efficiency. Other functions are useless.
The reusability layout is presented using. xml as follows:
[Java]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/app_bg"
Android: gravity = "horizontal">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/add"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/delete"/>
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/app_bg"
Android: gravity = "horizontal">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/add"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/delete"/>
</LinearLayout>
To avoid redundant layout elements, you can use <merge/> as the root View of the reusable layout file. For example:
Layout file using the <merge/> label:
[Java]
<Merge xmlns: android = "http://schemas.android.com/apk/res/android">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/add"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/delete"/>
</Merge>
<Merge xmlns: android = "http://schemas.android.com/apk/res/android">
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/add"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/delete"/>
</Merge>
Now, when you add the layout file (using the <include/> label), the system ignores the <merge/> node and directly adds two buttons
Replace <include/> nodes.