Daily summary-android layout optimization

Source: Internet
Author: User

1, avoid code duplication with <include/> Tags

Imagine a situation where we need to add a title to each view in the application. To simplify the problem, we assume that the title is a TextView that displays the application title. Often multiple activity will correspond to multiple XML files. Do we need to copy this textview into each XML file? What happens if I need to modify this textview later? The "copy/paste" approach can solve this problem, but it is not an efficient approach. The simplest way to solve these problems is to use the <include/> tag.

We can insert layouts defined in other XML files into the current layout file through the <include/> tag. As an example of one of the activity, the XML layout file looks like this:

1 <Relativelayout2   xmlns:android= "Http://schemas.android.com/apk/res/android"3   Android:layout_width= "Fill_parent"4   Android:layout_height= "Fill_parent">5 <TextView6    Android:layout_width= "Fill_parent"7    Android:layout_height= "Wrap_content"8    android:layout_centerinparent= "true"9    android:gravity= "Center_horizontal"Ten    Android:text= "@string/hello"/> One <includeLayout= "@layout/title"/> A  - </Relativelayout>

The title layout looks like this:

1 <TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Fill_parent"3 Android:layout_height= "Wrap_content"4 Android:layout_alignparentbottom= "true"5 Android:layout_marginbottom= "30DP"6 android:gravity= "Center_horizontal"7 Android:text= "@string/title_text"/>

In the example code above, we used the <include/> tag and only need to specify the layout property value for that label.

But there is also a problem, which is possible because the activity is using Relativelayout in the main layout file. What if one of the activity's layout files is using LinearLayout?

Although android:layout_alignparentbottom= "true" applies to relativelayout, it does not apply to LinearLayout. "The idea is correct. Next analysis
The second method of using the <include/> tag, in which we use the android:layout_* attribute directly in the <include/> tag.

The following is the modified Main.xml file, which uses the android:layout_* attribute of the <include/> Tag, the source code is as follows:

1 <Relativelayout2 xmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent">5   <TextView6     Android:layout_width= "Fill_parent"7 Android:layout_height= "Wrap_content"8 android:layout_centerinparent= "true"9 android:gravity= "Center_horizontal"Ten Android:text= "@string/hello"/> One   <include A     Layout= "@layout/title" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" the Android:layout_alignparentbottom= "true" - Android:layout_marginbottom= "30DP"/> - </relativelayout/>

The modified title layout file is as follows:

1 < TextView   xmlns:android = "Http://schemas.android.com/apk/res/android" 2   android:layout_width= "0DP"3  android:layout_height= "0DP"  4  android:gravity= "center"5  android:text  = "@string/title"/>

2, deferred Load for view via Viewstub

When designing layouts, the reader may want to display a view based on context or user interaction. If you want a view to be displayed only when you need it, you can use the Viewstub class.

An introduction to Viewstub is available in the Android development documentation, and the main content is as follows:

"Viewstub is an invisible, 0-size view that can be deferred to a run-time population (inflate) layout resource. When Viewstub is set to visual or the inflate () method is called, the layout resource is populated and the viewstub is replaced by the populated view. ”

Now that you know what viewstub is, let's see what it can do. In the example code below, we use viewstub to delay loading a mapview. Suppose you need to create a view to show the details of a geographic location, let's look at two possible scenarios:
? Some sites do not have GPS information
? Users may not need map information
If a site does not have GPS information, the developer does not need to display the tag information on the map. Similarly, if the user does not need map information, it is not necessary to load the map. We can place the Mapview in the Viewstub tab and let the user decide whether or not to display the map information.

To achieve this, you need to use the following layout:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent">5   <Button6     Android:layout_width= "Fill_parent"7 Android:layout_height= "Wrap_content"8 Android:text= "@string/show_map"9 Android:onclick= "Onshowmap"/>Ten   <viewstub One     Android:id= "@+id/map_stub" A Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" - Android:layout= "@layout/map" the Andorid:inflatedid= "@+id/map_view" /> - </Relativelayout>

Obviously, you need to get viewstub from activity by map_stub this ID. The Layout property also specifies the layouts file that needs to be populated. For this example, you need to fill in the Map.xml file, the source code is as follows:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Com.google.android.maps.MapViewxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:clickable= "true"6 Android:apikey= "My_api_key"/>


The last attribute to be described is Inflatedid. Inflatedid is the ID returned when calling Viewstub's inflate () method or the Setvisibility () method, which is the ID of the filled view. In this case, we don't need to manipulate Mapview, just call the Setvisibility (view.visible) method. If you want to get a reference to a filled view, the inflate () method returns the reference directly, avoiding the need to call the Findviewbyid () method again. The source code of the Activity is relatively simple, as follows:

1  Public classMainactivityextendsmapactivity {2 PrivateView mviewstub;3 @Override4  Public voidonCreate (Bundle savedinstancestate) {5     Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.main);7Mviewstub =Findviewbyid (r.id.map_stub);8 }9  Public voidOnshowmap (View v) {Ten mviewstub.setvisibility (view.visible); One } A ... -}

As shown in the code above, you can control the display of the map simply by changing the visibility of the viewstub.

Also available through view view = ((viewstub) Findviewbyid (r.id.map_stub)). Inflate ();

When the inflate () function is called,Viewstub is substituted for the referenced resource and returns the referenced view. This way the program can directly get the referenced view without calling the function Findviewbyid () again to find it.

Daily summary-android layout optimization

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.