[Android performance optimization series] layout Article reuse layout through (include)

Source: Internet
Author: User

[Android performance optimization series] layout Article reuse layout through (include)

 

In the next period of time, I will translate some official Android documents about performance improvement every day for you.

Performance Optimization Layout:

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

Digress:

Most of the time, we will use similar la S. In this case, we may wish to extract the same layout and use it as a layout file separately, in this way, we avoid repeatedly writing the same code in multiple files. When we need to modify the code, we only need to modify the code in one place.


 

The following is the body of this article:

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


Although Android provides us with a series of controls to facilitate interaction, you may need to reuse some large components with specific la S. You should use And To make one layout appear in another layout, instead of rewriting it in every layout file.

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

 

Create a reusable Layout

If you already know which la s you want to use repeatedly, create a new layout file for them. For example, there is a layout from the G-Kenya Code lab. It defines a title bar, which will be referenced by every activity.

 

<framelayout android:background="@color/titlebar_bg" android:layout_height="wrap_content" android:layout_width="”match_parent”" xmlns:android="http://schemas.android.com/apk/res/android">    
 </framelayout>

This View should be consistent with the display effect you want in each Activity.

 

 

Use Tag

When you want to add a reusable component to another layout, you can use Label. For example, there is a piece of code from the G-Kenya Code lab. He wants to include the title bar mentioned above.

Here is his layout File

 

     
      
   
        ...
   
  
 

Similarly, you can The parameters of the layout file are rewritten in the tag, for example, the property similar to android: layout _ *, to define the property of the contained layout, such

 

 

  

If you want to use You must override layout_height and layout_width to make other attributes take effect.

 

 

Use Tag

Labels can effectively help us lower your layout level. For example, your layout is a vertical linear layout, and the layout you want to reuse is also a similar vertical linear layout. Then, using another linear layout as the root element for reusing the layout will lead to a vertical linear layout containing another vertical linear layout. This nested linear layout does not make any sense and will reduce your ui performance.

To avoid the above situation, you can use it in your reusable layout. Label as the root element, as shown below

 

   
    
    
   
In this way, when you use When tags are reused, the system ignores them. Tag, and then place the two Button Go

 

Related Article

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.