This article original, reprint please indicate source: http://blog.csdn.net/qinjuning: Use <include/> Label Reuse layout file
Translation Address: Http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge
Although Android provides a small, reusable element of interactivity through a variety of built-in controls, you might need to reuse a larger
The component----some specific layout files. For more efficient reuse of layout files, you can add other layout files to the current layout file using <include/> and <merge/> tags. Reusing layout files is a particularly powerful method that allows you to create reusable layout files. For example, a button face version that contains "Yse" or "No", or a Progressbar with a text description. Reusing layout files also means that any element in your application can be managed separately from the cumbersome layout files, and then all you need to do is add these separate layout files (because they are reusable). Therefore, when you create a separate UI component from a custom view, you can reuse the layout file to make things easier.
1. Create a reusable layout file
If you already know the "face" of the reuse layout, create and define a layout file (named ". xml" as a suffix). For example, here is a layout file from G-kenya Codelab that defines a custom caption (Titlebar.xml) to be used in each activity: because these reusable layouts are added to other layout files, each of its root views (root View) is best for precision (exactly).
[Java]View Plaincopyprint?
- <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>
2. Use <include/> Label
Use the <include/> label where you need to add these layouts. For example, here is a layout file from G-kenya Codelab,
It re-uses the "title bar" file listed above, the layout file is as follows:
[Java]View Plaincopyprint?
- <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>
You can also define a special identifier for the root view of the added layout file in the <include/> node, overriding all layout parameters (any attribute prefixed with "Android:layout_"). For example:
[Java]View Plaincopyprint?
- <include android:id= "@+id/news_title"
- Android:layout_width= "Match_parent"
- android:layout_height= "Match_parent"
- layout= "@layout/title"/>
3. Use <merge/> Label
When reusing additional layouts in a layout file, the <merge/> tag removes extraneous view elements at the layout level. For example, if your
The main layout file is a linearlayout that contains two view vertically, which can be reused in other layouts, and requires a root View for any layout file that contains two view (otherwise, the compiler will prompt for errors). However, adding a linearlayout as root View in the reusable layout will result in a vertical linearlayout containing additional vertical linearlayout. Inline linearlayout can only slow UI efficiency, and others are useless. The reuse layout utilizes. XML to render as follows:
[Java]View Plaincopyprint?
- <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 a reusable layout file to root View. For example:
Layout file using the <merge/> Label:
[Java]View Plaincopyprint?
- <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/> tag), the system ignores the < merge/> node and adds two buttons directly to replace the <include/> node.
In addition, load view view on demand, see:
Http://developer.android.com/training/improving-layouts/loading-ondemand.html
To make the ListView slide smoothly, see:
Http://developer.android.com/training/improving-layouts/smooth-scrolling.html
View drawing optimization in Android 21----using <include/> Label Reuse layout file