Android mobile layout optimization tool include and viewstub

Source: Internet
Author: User

when creating complex layouts, it is sometimes found that a lot of viewgroup and view are added. The problem with this is that the view tree is getting deeper and more slowly, and the application becomes slower because the UI rendering is time-consuming.

this is where the layout optimization should be. Here are two ways to label and viewstub classes, respectively .

It is used to avoid duplication of code. Imagine a situation where we need to add a footer to each view in the app, a footer that shows the app's name TextView. Often multiple activity corresponds to multiple XML layout files, do you want to copy this textview into each XML? If TextView needs to be modified, then every XML layout file has to be modified, which is a nightmare.

One of the ideas of object-oriented programming is the reuse of code, so how do you reuse the layout? At this time, it worked.

It's also understandable in Android to copy some of the generic XML code to where it resides. Take an activity as an example.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXroeAwO21AAAtn7BOM0c568.png "height=" 190 "Width=" 493 "/>

Footer_with_layout_properties.xml is a simple textview, the code is as follows:

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXrpbhNmPrAAAVFZPmqDY996.png "/>

in the code above, we use tags to achieve the purpose of code reuse.

However, there are still some doubts.

The android:layout_alignparentbottom attribute is used in Footer_with_layout_properties.xml, which is possible because the outer layout is relativelayout.

So what happens if the outer layout changes linearlayout? The answer is obvious, it certainly won't work. So what do we do? We can write the specific attributes in the tag and see the code below.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXrrrAaw-lAAA8yvUp3rA691.png "height=" 233 "Width=" 497 "/>

we used the android:layout_* attribute directly inside the tag.

Note: If you want to overwrite any of the android:layout_* properties specified by the included layout in the label, you must specify both the Layout_width and Layout_height properties in the label, which can be a bug for an Android system.

viewstub

In the development process, you will inevitably encounter a variety of interaction problems, such as showing or hiding a view. If you want a view to be displayed only when you need it, try using the Viewstub class.

Let's take a look at Viewstub's official introduction:

"Viewstub is an invisible and size 0 view that can be deferred to the runtime to populate the layout resources. When Viewstub is set to visible or called inflate (), the layout resource is populated and the viewstub is populated with the view instead.

Now that you know what viewstub can do, look at an example. A layout, there is a mapview, only when it is necessary to show it.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXrrSzpM0NAABBO7oWlb8986.png "height=" 349 "Width=" 502 "/>

The map.xml file contains a mapview that is displayed only when necessary.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXrvTR9W78AAAcnNVuX8A954.png "/>

In addition, Inflatedid is the ID that is returned when Viewstub is set to visible or called the inflate () method, which is the ID of the filled view. In this case, it is the ID of the Mapview.

then take a look at how viewstub is used.

650) this.width=650; "src=" Http://183.61.143.148/group1/M00/02/11/tz2PlFQXruagwvU-AAAsJCtZNkc514.png "/>

Off Topic

Some students will certainly ask, use viewstub and simply to set the view to View.gone or view.visible what is the difference? Not all are shown and hidden, but using viewstub is more troublesome.

There is really a difference, it will involve the view tree rendering, memory consumption and so on. As for what the specific difference, please everyone to Baidu it. As the saying goes, own hands, clothed well! For more information on Mobile Internet Tutorials , please visit e-mentor Web .


Android mobile layout optimization tool include and viewstub

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.