The include tag in Android is a good solution to facilitate the overlay of controls. But there are some areas to be aware of, the following is the project I encountered a problem, to do this record, easy to view later. include label usage. 1. Create a new XML file, name the Head.xml head.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:id= "@+id/head_btn_refresh" android:layout_width= "wrap_content" and roid:layout_height= "Wrap_content" /> </relativelayout> 2. Create a new layout file, naming the Main.xml main.xml file with the following contents: <?xml version= "1.0" encoding= "Utf-8"? >&nbsP;<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, set the layout file to main.xml; 4. Suppose I now need toThe 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); this is OK. 5. Just said that 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. You need to use the unique properties of the Relativelayout layout container android:layout_below= "" property. You also need to specify the id attribute for the include. Our Main.xml file becomes as follows: <?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:layout_below=" @+id/main_headb " /> </relativelayout> then we are running our example, and the result is that the code throws an exception when it runs to the Head.setbackgroundresource (r.drawable.head); sentence java.lang.nullpointerexception Original: If the include specified the ID, it can not be directly in its control as a control in the main XML directly obtained, you must first obtain the XML layout file, The layout file is then Findviewbyid to get its child controls. The code is as follows view layout = Getlayoutinflater (). Inflate (r.layout.head, null); relativelayout head= ( Relativelayout) Layout.findviewbyid (r.id.index_linear_foot) //Set background image head.setbackgroundresource ( R.drawable.head); that's it.
Android include tag note