" original works, reproduced please indicate the source." ---Sun's prestige "
(Article original Address http://blog.csdn.net/manoel/article/details/39036507)
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 <include> tags and viewstub classes, respectively.
<include/>
<include/> 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 point the,<include/> worked.
If you learn C, then the # include should be familiar, it is a precompiled directive, before the program compiled into a binary file, will be included in the contents of the copy to the location of # include.
<include/> in Android is also able to understand that some generic XML code is copied to the place where <include/> is located. Take an activity as an example.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <textview android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:gravity= "Center_ Horizontal " android:text=" @string/hello "/> <include layout=" @layout/footer_with_layout_properties "/></relativelayout>
Footer_with_layout_properties.xml is a simple textview, the code is as follows:
<textview xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true" android:layout_ Marginbottom= "30DP" android:gravity= "center_horizontal" android:text= "@string/footer_text"/>
In the code above, we used the <include/> tag 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 <include/> tag and see the code below.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <textview android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:gravity= "Center_ Horizontal " android:text=" @string/hello "/> <include layout=" @layout/footer " android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_alignparentbottom= " True " android:layout_marginbottom=" 30DP "/></relativelayout>
We used the android:layout_* attribute directly in the <include/> tag.
Note: If you want to overwrite any of the android:layout_* properties specified by the included layout in the <include/> tab, you must specify both Layout_width and Layout_ in the <include/> tab Height property, which may 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.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent "> <button android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:onclick= " Onshowmap " android:text=" @string/show_map "/> <viewstub android:id=" @+id/map_stub " Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:inflatedid= "@+id/map_ View " android:layout=" @layout/map "/></relativelayout>
The Map.xml file contains a mapview that is displayed only when necessary.
<com.google.android.maps.mapview xmlns:android= "http://schemas.android.com/apk/res/android" android:id= " @+id/map_view " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android: apikey= "My_api_key" android:clickable= "true"/>
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.
Now let's see how viewstub is used.
public class Mainactivity extends Mapactivity { private View mviewstub; @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Mviewstub = Findviewbyid (r.id.map_stub); } public void Onshowmap (View v) { mviewstub.setvisibility (view.visible); } @Override protected Boolean isroutedisplayed () { return false; }}
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 go to Google yourself. As the saying goes, own hands, clothed well!
Resources
http://code.google.com/p/android/issues/detail?id=2863
Http://android-developers.blogspot.com.ar/2009/03/android-layout-tricks-3-optimize-with.html
Http://developer.android.com/reference/android/view/ViewStub.html
"Android Development Tips" Layout optimization tool <include/> and Viewstub