View drawing optimization in Android 21----using <include/> Label Reuse layout file

Source: Internet
Author: User

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?
  1. <framelayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. Android:layout_width= "Match_parent"
  3. android:layout_height="Wrap_content"
  4. android:background="@color/titlebar_bg" >
  5. <imageview android:layout_width="Wrap_content"
  6. android:layout_height="Wrap_content"
  7. android:src="@drawable/gafricalogo"/>
  8. </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?
  1. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. android:orientation="Vertical"
  3. Android:layout_width= "Match_parent"
  4. android:layout_height= "Match_parent"
  5. android:background="@color/app_bg"
  6. android:gravity="Center_horizontal" >
  7. <include layout="@layout/titlebar"/>
  8. <textview android:layout_width= "Match_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/hello"
  11. android:padding="10DP"/>
  12. ...
  13. </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?
    1. <include android:id= "@+id/news_title"
    2. Android:layout_width= "Match_parent"
    3. android:layout_height= "Match_parent"
    4. 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?
  1. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. android:orientation="Vertical"
  3. Android:layout_width= "Match_parent"
  4. android:layout_height= "Match_parent"
  5. android:background="@color/app_bg"
  6. android:gravity="Horizontal" >
  7. <button
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/add"/>
  11. <button
  12. Android:layout_width="Fill_parent"
  13. android:layout_height="Wrap_content"
  14. android:text="@string/delete"/>
  15. </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?
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" >
  2. <button
  3. Android:layout_width="Fill_parent"
  4. android:layout_height="Wrap_content"
  5. android:text="@string/add"/>
  6. <button
  7. Android:layout_width="Fill_parent"
  8. android:layout_height="Wrap_content"
  9. android:text="@string/delete"/>
  10. </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

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.