Android Siege Lion layout optimization

Source: Internet
Author: User
Tags set background

Android Common layout style 1.LinearLayout (linear layout) linear vertical Horizontal 2. Relativelayout (relative layout) The most flexible of 3. Tablelayout (table layout) is now replaced by the GridView 4.AbsoluteLayout (absolute layout) is obsolete 5. Framelayout (frame layout) Layout overlay uses now basically use linear layout and relative layout, special cases use frame layout (video pause, buffer file, etc.).

 android layout principle  1. Use LinearLayout (linear layout) and relativelayout (relative layout) as much as possible, Do not use Absolutelayout (absolute layout) 2. In the same layout hierarchy, it is recommended to use LinearLayout instead of relativelayout, since LinearLayout performance is better than relativelayout. 3. Extract the reusable components and use 4 with the include tag. Use the viewstub tag to load some infrequently used layouts 5. Use the Merge tab to reduce the nesting level of the layout. Flexible use of the following three principles will greatly optimize the layout of the project. ----------------------------------< include/> Use  Role: Extract the common components into a single XML file, and then use the Include tag to import the common layout. Effect: Improve UI production and reuse efficiency, but also ensure that the production UI layout is more regular and maintainable.  




  This section programming steps: 1. Create a relative layout common_title.xml. At the same time modify the theme of the Androidmanifest.xml application style properties, change: Android:theme= "@android: Style/theme.light.notitlebar" 2. The properties for adding 3 textview,textviwe in Common_title.xml are set to left-justified (layout_alignparentstart= "true"), centered (layout_centerinparent= " True ") and right-justified (layout_alignparentend=" true "). Both are set to center vertically (layout_centervertical= "true"), and the Relativelayout property layout_height= "Wrap_content" is modified so that the title bar is ready. 3. Add <include> tags to main.xml and introduce common_title.xml here: <include layout= "@layout/common_title"/> using include, It is equivalent to adding common_title to Main.xml as part of the main.xml. So you can use Findviewbyid (r.id.xxx) directly in mainactivity to find the controls inside the Common_title.  

  Merge UI layout with merge  role: Merging UI layouts , using this label can reduce the nesting level of the UI layout. Scene (1): Layout root node is framelayout and does not need to set background or padding properties, you can use merge instead; scene (2): When a layout is used as a sub-layout by another layout include, use merge as the top node of the layout. This will automatically be ignored when the top node is introduced. About the 2nd, for example, we create a new Common_progressbar.xml file, the root layout is LinearLayout, the inside set ProgressBar and TextView. Originally LinearLayout used a vertical layout, ProgressBar and TextView do not stack, but add framelayout sub-layouts in the Main.xml file and use include to add Common_ Progressbar.xml, and modify the root layout of the common_progressbar.xml, with merge instead of LinearLayout, the results appear ProgressBar and textview overlay effect, Indicates that the root layout that was included in the include is automatically ignored. In fact, it also proves the 1th. It is because of the use of the framelayout inside the include, in order to successfully replace the linearlayout with the merge. 
use viewstub lazy loading : The viewstub tag can be used as an include tag to introduce an external layout, but the layout introduced by the viewstub does not expand by default and does not occupy the display or occupy a position. This saves CPU and memory when parsing layout. So, you can artificially control when to load viewstub content. Viewstub is often used as an optimization that only displays content when an event is triggered and consumes CPU and memory. ----------------------Here we use buttons to implement viewstub lazy loading. 1. First create a new Common_text file and add only the TextView control. 2. Add the button and Viewstub in the Main.xml file, where viewstub Add the property android:layout= "@layout/common_text" to complete the layout. 3. Create and initialize the button and viewstub in Mainactiivity. Add listeners to the button to listen for click events. Rewrite the onclick () method: Add a sentence inside: viewstub.inflate ();//make lazy loading viewstub load layout-----------------You can hide content without using viewstub: for example, TextView has visibility property, setting visibility= "Gone" will not display the content. You can set a button listener, and when you click this button, you can change the visibility= "visible" to display the content. ----------------------from a comment area: viewstub It is best not to use inflate (), preferably with setvisibility (viewstub. Visible) display and use Setvisibility (viewstub. GONE) hidden, will be less trouble. But there are also shortcomings, that is, to occupy memory.
In addition, the "Show hidden Content button", click once, appear "hidden content", and then click the second time, the program directly flash back. Cause: The inflate operation on Viewstub can only be done once, because inflate is the layout file to parse inflate and replace the current viewstub itself (thus reflects the viewstub "placeholder" nature), once replaced, There is no viewstub control in the original layout file, so if you infalte the viewstub multiple times, an error message appears: Viewstub must has a non-null viewgroup viewparent.












Android Siege Lion layout optimization

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.