New UI-layout optimization highlights, newui-layout highlights

Source: Internet
Author: User

New UI-layout optimization highlights, newui-layout highlights

New UI-highlights of Layout Optimization

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group:Piggy Android Development GroupGroup Number:421858269

New Android UI instance catalog:Http://blog.csdn.net/coder_pig/article/details/42145907


This section introduces:


Three labels related to layout optimization have been introduced earlier:Include, ViewStub, merge;

I believe you have some knowledge about layout optimization. In the last section of this chapter, we will be nagging.

Some Suggestions on Layout Optimization! In actual development, you can refer to it to make it more efficient,

More reusable layout UI. Now let's get started with this section!



Body of this section:


1. The layout gives priority to RelativeLayout:

① When planning the layout of the outermost layer, we should give priority to RelativeLayout and never use AbsoluteLayout.

(Of course, basically no one will do this), either RelativeLayout or LinearLayout!


② When the layout layers are the same, that is, when the linear layout and the relative layout layers are the same, we recommend that you use LinearLayout because

The performance of the former is higher than that of RelativeLayout!


③ When the layout is complex, multiple nesting operations may be required to implement the effect using LinearLayout to achieve the same effect,

RelativeLayout only needs one layer. In this case, it is self-evident to use it!

Ps: Of course, you can use FrameLayout, TableLayout, and GridLayout based on different situations ~




2. Use three labels for Layout Optimization:

① Reusable components are extracted to a layout file and imported through the include tag.

② Layout may be used when it is not commonly used, or there are too many things on the page. Some parts can be temporarily unavailable and loaded after user interaction,

In this way, you can use <ViewStub>

③ For FrameLayout la S, you can use merge to replace or include the la S.

Merge acts as the outermost tag, which can reduce the view level!



3. Use less weight attributes of LinearLayout when embedding:

When nesting multiple layers, use the weight attribute of LinearLayout as little as possible, because the system requires a lot

System resources, because each sub-control needs to be measured twice, when using controls such as GridView and ListView,

This problem is even more serious because the child control will be repeatedly created. Therefore, when embedding multiple layers, try to avoid using the weight attribute!



4. Optimization of writing specifications:

Name IDs, resources, and general resources as standardized as possible, for example:

A login input box: login_edit_username

An added button: ic_add... and so on

Of course, you do not necessarily use the above method, but you must follow a standard.

In addition, we recommend that you set different IDs for different controls !!!




5. Improve the search efficiency of controls:

In the previous study, we can see through Hierarchy Viewer that when we load the layout file,

PhoneWindow $ DecorView is the control tree of the root node. When operating the control, we must find the corresponding control!

We usually find the corresponding control through findViewById in onCreate () of the Activity, and then initialize it!

Then perform related operations to set related properties or add event listening. The findViewById method starts from the root node of the Control tree.

Recursive search. Because of the particularity of the tree structure, we can select the control search policy based on the characteristics of the Control tree to reduce the number of searches

Optimized the search path!


① First look for the outer layer:

For example, a LinearLayout layout defines multiple Button controls, such:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 1"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 2"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 3"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 4"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 5"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 6"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 7"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 8"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button 9"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "button 10"/> </LinearLayout>


If you need to initialize all the buttons, how do you write them? Most friends may first set an id for each button

Write it like this:

Button btn1 = (Button) findViewById(R.id.btn1);Button btn2 = (Button) findViewById(R.id.btn2);Button btn3 = (Button) findViewById(R.id.btn3);Button btn4 = (Button) findViewById(R.id.btn4);Button btn5 = (Button) findViewById(R.id.btn5);Button btn6 = (Button) findViewById(R.id.btn6);Button btn7 = (Button) findViewById(R.id.btn7);Button btn8 = (Button) findViewById(R.id.btn8);Button btn9 = (Button) findViewById(R.id.btn9);Button btn10 = (Button) findViewById(R.id.btn10);


Complete related operations on each Button, but you will surely find that the code is too repetitive and many variables are defined.

For buttons, we usually only set a click event for them, so we can look for controls like this:

Find the container control of the external LinearLayout, call the getChildAt method to obtain the button control, and complete initialization:

ViewGroup parent = (ViewGroup) findViewById (R. id. parent); int count = parent. getChildCount (); for (int I = 0; I <count; I ++) {Button button = parent. getChildAt (I); // perform related operations for the Button}

Obviously, the code of the latter is more advantageous because of the Code of the former, and the more child controls, the more layers the control tree;

From the improved code, we can find another more important principle:

When searching for controls, try to start from the node closest to the control!



② Start from the node closest to the control

You need to find a widget in a deeply nested layout, such as three layers in a Button and TextView.

In nested LinearLayout, how do you find these two components? Direct:

Button btn = (Button)findViewById(R.id.btn);TextView txt = (TextView)findViewById(R.id.txt);
The above statement can certainly make up the work, but when the control nesting level is deep, and the number of queries for the control is large

This method reduces the query performance of controls. In this case, you only need to add a line of code

Optimization:

View parent = findViewById (R. id. parent); // locate the node where the control is located (front container)

Add this statement before the component search. When the number of controls is large, its advantages will be obvious, and in some cases

It must be written in this way. Although it is not recommended that you set the same id for different controls, when using a space such as ListView

This will continue, and the findViewById search policy is: Find the first control that matches the id and stop searching, so

The search result may not be the space actually needed. Therefore, starting from the node close to the control, it not only improves the search speed, but also

We recommend that you set different id values for different controls !!!

--- The fifth part from: http://www.devdiv.com






Last few words:

There are certainly more things about layout optimization than above, which are limited to the level of the author. Now I know this. What new things will happen in the future?

I will share with you what I learned. In addition, the optimization of ListView and other controls will not be carried out in this article.

Elaborate, see the content of the listView chapter! Thank you ~










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.