Android include labels

Source: Internet
Author: User
In Android, The include tag is a good solution to facilitate control overwriting. However, there are also some points to note. Below is a problem I encountered in the project. Make this record for later viewing. Include label usage. 1. Create an XML file named head. xml head. xml: <? 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: Id = "@ + ID/head_btn_refresh" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </relativelayout> 2. create a layout file named main. XML main. the content of the XML file is as follows: <? 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: in the preceding include tag, no ID is specified for it. 3. Create a mainactivity and set the layout file to main. xml. 4. Assume that you need Code Set the background image for the relativelayout container in head. xml. The Code is as follows: // obtain the layout container object relativelayout head = (relativelayout) findviewbyid (R. id. index_linear_foot); // sets the background image head. setbackgroundresource (R. drawable. head); then OK. 5. we just mentioned that our include label does not specify an ID for it. Suppose our current main. the XML file layout container is relativelayout, and I need to put a control in the head. XML. You need to use the special property Android: layout_below = "" of the relativelayout layout container. You also need to specify the ID attribute for include. The main. xml file is changed to the following: <? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <include Android: Id = "@ + ID/main_head" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" layout = "@ layout/head"/> <listview Android: Id = "@ + ID/listview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: l Ayout_below = "@ + ID/main_headb"/> </relativelayout> then we are running our instance. The result shows that the code is running to the head. setbackgroundresource (R. drawable. head); this statement throws an exception in Java. lang. nullpointerexception: If the include parameter specifies an ID, you cannot directly use the control in it as a control in the main XML. You must first obtain the XML layout file, then obtain its child control through the layout file findviewbyid. The Code is as follows: View layout = getlayoutinflater (). inflate (R. layout. head, null); relativelayout head = (relativelayout) layout. findviewbyid (R. id. index_linear_foot); // sets the background image head. setbackgroundresource (R. drawable. head.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.