Android Layout Performance optimization

Source: Internet
Author: User

The essence is to achieve performance optimization by reusing controls.

1. Through <Include/>

Reused controls:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" wrap_content "    android:o rientation= "vertical"     android:background= "#ccc"    >    <textview         android:gravity= "center"         android:layout_width= "match_parent"    android:layout_height= "wrap_content"    android:text= " Using the Include multiplexing control "    android:textsize=" 15sp "    android:textcolor=" @android: Color/holo_red_light "        />    </LinearLayout>


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "android:orientation=" vertical "> <textview android:id=" @+id/textview1 "Android    : layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/> <quickcontactbadge android:id= "@+id/quickcontactbadge1" android:layout_width= "Wrap_content" Android        oid:layout_height= "Wrap_content" android:layout_below= "@+id/textview1" android:layout_margintop= "48DP" android:layout_torightof= "@+id/textview1"/> &Lt ProgressBar android:id= "@+id/progressbar1" style= "? Android:attr/progressbarstylelarge" Android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignright= "@+id/quickContactBadge 1 "android:layout_below=" @+id/quickcontactbadge1 "android:layout_marginright=" 34DP "Android:layout_ma rgintop= "37DP"/> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" Android oid:layout_height= "Wrap_content" android:layout_alignleft= "@+id/progressbar1" android:layout_below= "@+id/pro GressBar1 "android:layout_margintop=" 26DP "android:text=" button "/> <include layout=" @layout/bottom "Android:id=" @+id/myresue "android:layout_alignparentbottom=" true "/></linearlayout>
Re-use effect:



2 ' through the merge

Resolves a problem where layout nesting degrades performance. Extract the controls at the consent level in a layout, and in the include, improve performance.

<?xml version= "1.0" encoding= "Utf-8"? ><merge xmlns:android= "Http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent"   android:layout_gravity= " Center_vertical "     >    <button        android:id=" @+id/button1 "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:text= "Button1"/>    <button        android:id= "@+id /button2 "        android:layout_width=" match_parent "        android:layout_height=" wrap_content "        android:text = "Button2"/></merge>


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:or         ientation= "Horizontal" android:background= "#f00" > <textview android:gravity= "Center" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "using the merge multiplexing control" Android:t     Extsize= "15SP" android:textcolor= "@android: Color/holo_red_light"/> </LinearLayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:o rientation= "Horizontal" android:background= "#0f0" > <textview android:gravity= "Center" Android:layout_widTh= "Match_parent" android:layout_height= "wrap_content" android:text= "using the merge multiplexing control" android:textsize= "15sp" Android         Oid:textcolor= "@android: Color/holo_red_light"/> </LinearLayout> <linearlayout      Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" android:background= "#00f" > <textview android:gravity= "center" Android:lay    Out_width= "Match_parent" android:layout_height= "wrap_content" android:text= "using the merge multiplexing control" android:textsize= "15SP" Android:textcolor= "@android: Color/holo_red_light"/> </LinearLayout> <linearlay Out android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "vertic Al "> <include layout=" @layout/mybutton "/> </LinearLayout> </linearlayou T>

Effect:



Android Layout Performance optimization

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.