Android include tags

Source: Internet
Author: User

The programming world sometimes is very subtle, sometimes it seems to solve a philosophical problem, Android development, all the layout, color, etc. (in fact, these can be called resources, Android Resources refers to non-code parts, tablets, audio, video, characters and other resources, In fact, some of the objects that can be manipulated by the code can be laid out in an XML file, and all operations on the XML can be performed in the corresponding activity, which seems to separate the programmer from the artist.

In a system, there are often duplicate interfaces, or partitioned logical units that can be reused, and this time the solution tends to be to do one later and then copy. An include tag is available in Android to solve the reuse problem.

Include label usage. 1. Create a new XML file, naming the Head.xmlhead.xml file with the following contents: <?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android = "Http://schemas.android.com/apk/res/android" android:id= "@+id/index_linear_foot" android:layout_width= "fill _parent "android:layout_height=" wrap_content "android:orientation=" horizontal "> <imageview android:i        D= "@+id/head_btn_refresh" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" /></relativelayout>2. Create a new layout file, name the Main.xmlmain.xml file with the following contents: <?xml version= "1.0" encoding= "Utf-8"?><    LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" ><include android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "layout=" @layout/head "/></linearlayout> Note: There is no ID specified for it in our include tag above. 3. Create a new mainactivity and set the layout file to Main.xml;4. Let's say I'mYou need to set a background picture in your code for the Relativelayout container in Head.xml. The code is as follows:
Get Layout Container object relativelayout head = (relativelayout) Findviewbyid (r.id.index_linear_foot);// Set the background picture Head.setbackgroundresource (r.drawable.head); so it's OK. 5. As stated above, our include tag does not specify an ID for it, assuming that our current Main.xml file layout container is relativelayout, and I need to place a control under Head.xml. However, if the ID tag is set for Inlclude, the above code will fail to run.

<!--the id attribute in the Include tag (ANDROID:ID), the id attribute in the layout layouts referenced does not work, you need to get the layout file, and then get the object inside with that layout file--

  

The relativelayout layout is used, and the include tag is used in the layout if the Layout_width and Layout_height properties are not overloaded at the same time.
Other layout_* properties are ignored unless both properties are overloaded at the same time.
Another way is to use the LinearLayout packaging.

Reference: http://blog.csdn.net/race604/article/details/7564088

Android include tags

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.