Some of the layout optimization tips you need to know

Source: Internet
Author: User
Tags xmlns

Reprint Please specify source: http://blog.csdn.net/qq_17766199/article/details/52863741

Share some of the tips of layout writing today, and hopefully you'll be able to write a cost-effective layout as well. My personal goal is to write the same effect layout with a minimal view. Because I believe that the reduction in the number of view is accompanied by a reduction in the level. So as to achieve a clear structure, rendering speed of the effect. Along this logic, I will optimize into reuse, merge, on-demand loading. Reuse < include/>

< include> tags can introduce another layout in one layout, the benefits of which are obvious. Similar to the tool class we often use, with Tiaogan. Easy to use for uniform modification.

Example: First write a common layout title_bar.xml,app in the common title bar.

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "Android:background
        = "@color/background" android:layout_height= "48DP" > <imageview android:layout_width= "wrap_content"
        android:layout_height= "Match_parent" android:paddingleft= "15DP" android:paddingright= "15DP" android:src= "@drawable/icon_back"/> <textview tools:text= "title android:layout_width=" Wrap_conte NT "android:layout_height=" Wrap_content "android:layout_centerinparent=" true "android:textsize=" 18 SP "android:textcolor=" @color/white "/> <textview tools:text=" OK "android:layout_width= "Wrap_content" android:gravity= "center" android:layout_height= "Match_parent" ANDROID:LAYOUT_ALIGNP Arentright= "true" android:paddingleft= "15DP" android:paddingright= "15DP" android:textsize= "16sp" android:textcolor= "@c
      
      Olor/white "/> </RelativeLayout> 1 2 3 4
      
      5 6 7 8 9 10 11 12
      
      13 14 15 16 17 18 19 20
      
      21 22 23 24 25 26 27
     
     
     
     
      
      
     
     
     
     
      
      28 29 30 31 32 33 34 35
      
      1 2 3 4
      
      5 6 7 8 9 10 11 12
      
    13  14 15 16 17 18 19 20 21 
      
      22 23 24 25 26 27 28 29
     
      30 31 32 33 34 35

Preview:

Down Activity_main.xml call it:

<?xml version= "1.0" encoding= " Utf-8 "?> <relativelayout xmlns:android=" http://schemas.android.com/apk/res/android "Android:layout_width=" M Atch_parent "android:layout_height=" match_parent "> <include layout=" @layout/title_bar "/> </R
      
      Elativelayout> 1 2 3 4 5 6
     
     
     
     
      
      
     
     
     
     
      
      7 8 9) 10 11
      
      1 2 3 4 5 6 7 8 9 

The post-run effect is the same as the preview. Of course we can also reset the width higher layout property in the < include> tab. merge reduce nesting

First of all we have a big principle in mind: try to keep the layout level flat. In this big principle we need to know:

Use LinearLayout instead of relativelayout without affecting the depth of the hierarchy. Because Relativelayout will let the child view call 2 times onmeasure,linearlayout when there is weight, the child view will be called 2 times onmeasure. The longer the measure, the less efficient it is to draw.

If it is not nested, try to avoid relativelayout nesting relativelayout. This is a vicious circle, a lunatic.

implementation of the method is not elaborate, everyone is sensible. < merge/>

< merge/> is mainly used to remove unnecessary framelayout. The best thing about it is that your root layout is framelayout, and you don't use attributes such as background. This can be replaced directly. Because the outer layer of our layout is framelayout, "merge"directly.

Example: For example, the Activity_main.xml file used above, we look through the view Hierarchy tool, as shown:

As you can see, the outermost layer is framelayout, and we revise it down.

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

    <include
        layout= "@layout/title_bar"/>

</merge>
     
     
      
      1
      
      2
      
      3
      
      4
      
      5
      
      6
      
      7
      
      8
      
      9
     
     
     
     
      
      
     
     
     
     
      
      1
      
      2
      
      3
      
      4
      
      5
      
      6
      
      7
      
      8
      
      9
      
      10
     
     

Check again:

Obviously less a layer of relativelayout, of course, the effect is the same. Of course, if we don't need the green background in title_bar.xml, we can change that.

<?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" > <imageview android:layout _width= "Wrap_content" android:layout_height= "48DP" android:paddingleft= "15DP" android:paddingright = "15DP" android:src= "@drawable/icon_back_1"/> <textview android:layout_gravity= "Center_horizont Al "android:text=" title "android:gravity=" Center "android:layout_width=" Wrap_content "Android : layout_height= "48DP" android:layout_centerinparent= "true" android:textsize= "18SP" Android:textcol Or= "@color/black"/> <textview android:text= "OK" android:layout_gravity= "right" Android : layout_width= "wrap_content" android:gravity= "center" android:layout_height= "48DP" Android:layout_
Alignparentright= "true"        android:paddingleft= "15DP" android:paddingright= "15DP" android:textsize= "16SP" Android:tex
      
      Tcolor= "@color/black"/> </merge> 1 2 3 4
      
      5 6 7 8 9 10 11 12 
      
      13 14 15 16 17 18 19 20
      
      21 22 23 24 25 26 27
      
      28 29 30 31 32 33 34 35
      
      36 1 2 3
      
      4 5 6 7 8 9 10 11
      
   12   13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2
      
      8 29 30 31 32 33 34 35
     
      36

Operating effect:

Run the view hierarchy as shown below:

The result is obvious. display pictures and text at the same time with TextView

I'm not going to go into this, give an example of our project, and the code will look at it.

The first thing to accomplish is the following figure:

This effect is common, as is the general implementation method. (no one seems to write like that, haha)

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
    

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.