This article is original, reproduced please indicate the source: http://blog.csdn.net/qinjuning
Translation 2:
Reuse layout files with <include/> labels
Translation address:Http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge
Although Android provides tiny and reusable interactive elements through various built-in controls, you may need to reuse large
Components-some specific layout files. To reuse layout files more efficiently, you can use the <include/> and <merge/> labels 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, a button panel 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 extracted from complicated layout files for separate management, all 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, here is
The layout file of codelab defines a custom title (titlebar. XML): Since these reusable la s are added to other layout files, it is recommended that each of its root views be accurate (exactly.
<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 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:
<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 and override all the layout parameters (any: layout _ "is a prefix attribute ). For example:
<include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>
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. A root view is required for any layout file containing two views (otherwise, the compiler will prompt an error ). However, adding a linearlayout to the reusable layout as the root view will result in a vertical linearlayout containing another vertical linearlayout. Embedded linearlayout can only reduce UI efficiency, and other functions are useless. The reusability layout is presented using. XML as follows:
<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:
<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 to replace the <include/> node.
In addition, to load the view as needed, see:
Http://developer.android.com/training/improving-layouts/loading-ondemand.html
For details about how to smoothly slide the listview, see:
Http://developer.android.com/training/improving-layouts/smooth-scrolling.html