[Android performance Optimization series] layout of the passage <include> reuse layout

Source: Internet
Author: User

If you like my blog, please follow my Weibo, please click here (http://weibo.com/kifile), thank you

Reprint please indicate the source (http://blog.csdn.net/kifile), thanks again


Original address: http://developer.android.com/training/improving-layouts/reusing-layouts.html

For the next period of time, I will translate some of the official Android documents for performance improvement every day to everyone

Performance Optimization Layout Chapter:

[Android performance Optimization series] layout to reduce your interface level

Off Topic:

Most of the time, we use a similar layout, so we might as well pull out the same layout as a single layout file, so we avoid repeating the same code in multiple files, and when we need to change it, we just need to change one place.


Here is the text of this session:

################


While Android provides us with a range of controls to facilitate our interaction, you may still need to reuse large components for specific layouts. For a more efficient reuse of layouts, you should use <include/> and <merge/> to have one layout appear in another layout instead of rewriting them in every layout file.

In this case, reusing the layout is particularly useful, and he allows you to create a complex, reusable layout. For example, a yes/no button, a custom progress bar with text. This also means that some of the elements in your application are generic. So you can create a custom View for them individually so you can reuse layouts more easily


Create a reusable layout

If you already know which layouts you want to be able to use repeatedly, create a new layout file for them alone. For example, here is a layout from the G-kenya Code lab that defines a title bar that is referenced by each activity

<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>

This View should be consistent with what you want him to show in each Activity.


Use <include> tags

When you want to add a reusable component to another layout, you can use the <include/> tag. For example, here's a code from the G-kenya Code lab that wants to include the title bar mentioned above.

Here's his layout file.

<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>

Similarly, you can override the parameters of the layout file within the <include/> tag, such as android:layout_*-like properties, to define the properties of the contained layout, for example

<include android:id= "@+id/news_title"         android:layout_width= "match_parent"         android:layout_height= " Match_parent "         layout=" @layout/title "/>

anyway, if you want to use the <include> tag to rewrite layout properties, you have to rewrite Layout_height and layout_width for other properties to take effect


Use <merge> tags

<merge/> tags are effective in helping us to lower your layout level. For example, your layout is a vertical linear layout, and the layout you want to reuse is a similar vertical linear layout. Then, using another linear layout as the root element of the reuse layout causes a vertical linear layout to contain another vertical linear layout. This nested linear layout has no meaning and can degrade your UI performance

To prevent this from happening, you can use the <merge> tag as the root element in the layout you reuse, such as the following

<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>
This way, when you use the <include/> tag for reuse, the system ignores the <merge> tag and then puts two button buttons in the <include/> location.

[Android performance Optimization series] Layout passage <include> reuse layout

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.