Android development tips include avoiding code duplication

Source: Internet
Author: User

In the layout, often some of the parts are repeated, such as title or foot place, the simplest way is to copy the past directly,

The use of include is described here, and students with C + + or C experience can see that the other layout is included.

First look at the effect of the implementation:

Inside and out there are two text layout, is included in the include, directly see the code

Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <include        android:layout_width= "fill_parent"        android:layout_height= "Wrap_content        " Android:layout_alignparenttop= "true"        android:layout_margintop= "30DP"        layout= "@layout/header"/>    <viewstub        android:id= "@+id/pic_stub"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:layout_centerinparent= "true"        android:inflatedid= "@+id/pic_view_id_ After_inflate "        android:layout=" @layout/pic_view "/>    <include layout=" @layout/footer "/></ Relativelayout>


Very clear two include, as for the middle of the viewstub, in the next blog to explain

Address: http://blog.csdn.net/jason0539/article/details/26132267

I used two include in the code.

The first one is more complex, set the property so that it's at the top of the layout

The second one is concise, the properties are not set, but the location is correct, at the bottom of the layout,

The purpose of this is to show the two settings of the Include attribute, which can be set in the include tag or in the layout to be referenced

The two layout files that are included are as follows:

Header.xml

<textview xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "0DP    " android:layout_height= "0DP"    android:gravity= "center_horizontal"    android:text= "Header without Properties!" ></TextView>


The width height of the polygon is set to 0, which is to set its layout in the layout with the include tag.

Footer.xml

<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= "with properties!"/>

The properties of this set are very complete, so in the layout of the include tag is nothing set, direct reference on the line,

The two methods have the same effect and look like a person's preference.

jason0539

Weibo:http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprint please specify source)

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.