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.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:id= "@+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, set the layout file to main.xml; 4. Suppose I now need to set a background picture in my 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. 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. Then 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 "/><listviewandroid: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 we find that The code is running to Head.setbackgroundresource (r.drawable.head); This sentence is thrown out of the ordinary java.lang.nullpointerexception Original: If the include specified the ID, you can not directly use it as a control in the main XML directly obtained, you must first obtain the XML layout file, and then through the layout file Findviewbyid to obtain its child controls. The code looks like view layout = Getlayoutinflater (). Inflate (r.layout.head, NULL); Relativelayout head= (relativelayout) Layout.findviewbyid (r.id.index_linear_foot);// Set the background picture Head.setbackgroundresource (R.drawable.head);
Include tag usage in Android